Python Forum
[pandas] convert Int to str - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: [pandas] convert Int to str (/thread-19294.html)



[pandas] convert Int to str - Scott - Jun-21-2019

Hi everyone,

How do I convert an int to a string in Pandas?

data['Hs_code'] = data.Hs_code.astype(str)
this is my current attempt, any help is appreciated.

Thanks


RE: convert Int to str - Larz60+ - Jun-22-2019

data_str = f"{data['Hs_code']}"


RE: convert Int to str - ThomasL - Jun-22-2019

What´s your problem with it?
pandas.series.astype()


RE: [pandas] convert Int to str - Scott - Jun-22-2019

I get this error when I try is astype:

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

Thanks Larz60+, why does astype not work? would make things a lot easier :(


RE: [pandas] convert Int to str - Larz60+ - Jun-23-2019

Quote:Thanks Larz60+, why does astype not work? would make things a lot easier :(
Sorry, I don't have the answer.
Please note that this forum is not directly associates with python.org (authors of python)


RE: [pandas] convert Int to str - scidam - Jun-23-2019

Can you provide minimal code to reproduce the problem. I tried to reproduce the issue and found
that it can be achieved when, e.g.

import pandas as pd
df = pd.DataFrame({'x' : [1,2,3,4]})
df.x.str #  This yield the error.
The problem is raising when you're trying to access string methods for the column of floating data type.
So, you need to convert the column to np.object or str dtype, e.g. df.x = df.x.astype(str)

import pandas as pd
df = pd.DataFrame({'x' : [1,2,3,4]})
df.x = df.x.astype(str)
df.x.str #  No errors!



RE: [pandas] convert Int to str - ichabod801 - Jun-23-2019

(Jun-23-2019, 11:09 AM)scidam Wrote: Can you provide minimal code to reproduce the problem.

This would be really useful. If you are converting to string to do a string operation on the column, there might be an equivalent integer operation that could be done on the column directly, which would probably be faster.


RE: [pandas] convert Int to str - Scott - Jun-24-2019

I think you are correct.

Basically I am trying to sum up imports when Hs_code starts with 08.

data.loc[data.Hs_code.str[:2] == '08'].Imports_vfd.sum()
This code
data['Hs_code'] = f"{data['Hs_code']}"
converted it to a string but put an array of numbers in each input.

Does anyone know how I can do a slice on the first two digits of the int column (HS_code) and if those first two number = 02 then sum up imports_vfd.

Thanks for any help

Thanks scidam - your one worked. You'd think this would be a common issue and you could find the answer online easier :'(


RE: [pandas] convert Int to str - scidam - Jun-24-2019

(Jun-24-2019, 07:05 AM)Scott Wrote: Does anyone know how I can do a slice on the first two digits of the int column (HS_code) and if those first two number = 02 then sum up imports_vfd.

If the column is of integer type, any number in it can't start with 0. So, Hs_code seems to be of string dtype.

Hope the following example helps you:

import pandas as pd
df = pd.DataFrame({'x': ['02', '34', '02'], 'y':[1,2,3]})
df[df.x.str.startswith('02')].y.sum()
Note, trying to initialize the df as follows
df = pd.DataFrame({'x': [02, 34, 02], 'y':[1, 2, 3]})
will yield an error.