Categories
Python Answers

How to convert number words to integers with Python?

Sometimes, we want to convert number words to integers with Python.

In this article, we’ll look at how to convert number words to integers with Python.

How to convert number words to integers with Python?

To convert number words to integers with Python, we can use the word2number module.

To install it, we run:

pip install word2number

Then we write:

from word2number import w2n

n = w2n.word_to_num("ten million three thousand nine hundred and twenty four")
print(n)

We call w2n.word_to_num with the English number string that we want to convert into a number.

Then we assign the returned number to n.

Therefore, n is 10003924.

Conclusion

To convert number words to integers with Python, we can use the word2number module.

Categories
Python Answers

How to convert a binary number string to an int with Python?

Sometimes, we want to convert a binary number string to an int with Python.

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

How to convert a binary number string to int with Python?

To convert a binary number string to an int with Python, we can call the int function with the number string to convert and base 2.

For instance, we write:

n = int('11111111', 2)
print(n)

We call int with '11111111' and 2 to convert '11111111' to a decimal int and assign that to n.

Therefore, n is 255.

Conclusion

To convert a binary number string to an int with Python, we can call the int function with the number string to convert and base 2.

Categories
Python Answers

How to add Python Pandas data to an existing CSV file?

Sometimes, we want to add Python Pandas data to an existing CSV file.

In this article, we’ll look at how to add Python Pandas data to an existing CSV file.

How to add Python Pandas data to an existing CSV file?

To add Python Pandas data to an existing CSV file, we can use the df.to_csv method.

For instance, we write:

import pandas as pd

df = pd.read_csv('foo.csv', index_col=0)
with open('foo.csv', 'a') as f:
    (df + 5).to_csv(f, header=False)

to read the content of foo.csv with read_csv.

Then we call open with 'foo.csv' to open the file with 'a' permission, which is append.

And we call to_csv on df + 5 with file f to append the new values to foo.csv.

We set header to False to skip adding headers.

Therefore, if foo.txt originally has:

,A,B,C
0,1,2,3
1,4,5,6

Then we get:

,A,B,C
0,1,2,3
1,4,5,6
0,6,7,8
1,9,10,11

after calling to_csv on df + 5.

Conclusion

To add Python Pandas data to an existing CSV file, we can use the df.to_csv method.

Categories
Python Answers

How to get list of values from a Python dict?

Sometimes, we want to get list of values from a Python dict.

In this article, we’ll look at how to get list of values from a Python dict.

How to get list of values from a Python dict?

To get list of values from a Python dict, we can use the dict’s values method and the list function.

For instance, we write:

d = {'a': 1, 'b': 2}
v = list(d.values())
print(v)

We call d.values to return a view of the values.

Then we use the list function to convert the values view into a list.

Therefore, v is [1, 2].

Conclusion

To get list of values from a Python dict, we can use the dict’s values method and the list function.

Categories
Python Answers

How to convert an XML file to a Pandas DataFrame?

Sometimes, we want to convert an XML file to a Pandas DataFrame.

In this article, we’ll look at how to convert an XML file to a Pandas DataFrame.

How to convert an XML file to a Pandas DataFrame?

To convert an XML file to a Pandas DataFrame, we can use the xml.etree.ElementTree module.

For instance, we write:

import pandas as pd
import xml.etree.ElementTree as ET

xml_str = '''<?xml version="1.0" encoding="utf-8"?>
<response>
  <head>
    <code>   200  </code>
  </head>
  <body>
    <data id="0" name="All Categories" t="2018052600" tg="1" type="category"/>
    <data id="13" name="RealEstate.com.au [H]" t="2018052600" tg="1" type="publication"/>
  </body>
</response>
'''

etree = ET.fromstring(xml_str)
dfcols = ['id', 'name']
df = pd.DataFrame(columns=dfcols)

for i in etree.iter(tag='data'):
    df = df.append(pd.Series([i.get('id'), i.get('name')], index=dfcols),
                   ignore_index=True)

h = df.head()
print(h)

We have an XML string assigned to xml_str.

And we parse it by passing that as the argument of ET.fromstring.

Next, we define the columns of the DataFrame.

And we create the DataFrame with the DataFrame constructor.

Next, we loop through the parsed XML data elements we got with etree.iter(tag='data') with the for loop.

And we call df.append to append to id and name attribute values by putting them into a series.

Then we get the first 5 rows with df.head.

Therefore, print should print:

   id                   name
0   0         All Categories
1  13  RealEstate.com.au [H]

Conclusion

To convert an XML file to a Pandas DataFrame, we can use the xml.etree.ElementTree module.