Categories
Python Answers

How to convert a Python int into a binary string?

Sometimes, we want to convert a Python int into a binary string.

In this article, we’ll look at how to convert a Python int into a binary string.

How to convert a Python int into a binary string?

To convert a Python int into a binary string, we can use the format method.

For instance, we write

s =  "{0:b}".format(88)

to convert 88 to a binary string by calling "{0:b}".format with 88.

Conclusion

To convert a Python int into a binary string, we can use the format method.

Categories
Python Answers

How to create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas?

Sometimes, we want to create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas.

In this article, we’ll look at how to create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas.

How to create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas?

To create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas, we can use the data frame apply method.

For instance, we write

import pandas as pd

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df['c'] = df.apply(lambda row: row.a + row.b, axis=1)

to call df.apply with a function that adds the value from columns a and b row-wise and assign the values to column c.

Conclusion

To create new column based on values from other columns or apply a function of multiple columns, row-wise with Python Pandas, we can use the data frame apply method.

Categories
Python Answers

How to get the filename without the extension from a path in Python?

Sometimes, we want to get the filename without the extension from a path in Python.

In this article, we’ll look at how to get the filename without the extension from a path in Python.

How to get the filename without the extension from a path in Python?

To get the filename without the extension from a path in Python, we can use the os.path.splitext method.

For instance, we write

import os

print(os.path.splitext("/path/to/some/file.txt")[0])

to call os.path.splitext with the path to get the filename for.

Then we get the filename without the extension by getting the first part of the split path string list.

Conclusion

To get the filename without the extension from a path in Python, we can use the os.path.splitext method.

Categories
Python Answers

How to check if a variable exists with Python?

Sometimes, we want to check if a variable exists with Python.

In this article, we’ll look at how to check if a variable exists with Python.

How to check if a variable exists with Python?

To check if a variable exists with Python, we can check with various functions.

For instance, we write

if 'my_var' in locals():
  # ...

to call locals to return a dict with all the local variables.

We use 'my_var' in locals() to check if my_var is a local variable in the current scope.

Likewise, we call globals to return a dict with a list of global variables.

For instance, we write

if 'my_var' in glocals():
  # ...

to check if the my_var variable is a global variable in the current module.

To check if an attribute is in an object, we use hasattr.

For instance, we write

if hasattr(obj, 'attr_name'):
  # ...

to call hasattr to check if the attr_name attribute is in the obj object.

Conclusion

To check if a variable exists with Python, we can check with various functions.

Categories
Python Answers

How to extract an attribute value with Python beautifulsoup?

Sometimes, we want to extract an attribute value with Python beautifulsoup.

In this article, we’ll look at how to extract an attribute value with Python beautifulsoup.

How to extract an attribute value with Python beautifulsoup?

To extract an attribute value with Python beautifulsoup, we can use the list of dicts returned by findAll.

For instance, we write

import urllib

f = urllib.urlopen("http://example.com")
s = f.read()
f.close()

from BeautifulSoup import BeautifulStoneSoup

soup = BeautifulStoneSoup(s)

input_tags = soup.findAll(attrs={"name": "stainfo"})

output = [x["stainfo"] for x in input_tags ]

print(output)

to parse the HTML returned from urlopen with

soup = BeautifulStoneSoup(s)

Then we get all the elements with the attribute name stainfo with

soup.findAll(attrs={"name": "stainfo"})

And then we get the stainfo attribute value of each element with

[x["stainfo"] for x in input_tags ]

Conclusion

To extract an attribute value with Python beautifulsoup, we can use the list of dicts returned by findAll.