Python Forum
[pandas] convert Int to str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[pandas] convert Int to str
#1
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
Reply
#2
data_str = f"{data['Hs_code']}"
Reply
#3
What´s your problem with it?
pandas.series.astype()
Reply
#4
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 :(
Reply
#5
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)
Reply
#6
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!
Reply
#7
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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 :'(
Reply
#9
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [pandas] Convert categorical data to numbers pradeep_as400 1 2,640 Jun-15-2019, 08:27 AM
Last Post: ThomasL
  Python read Password protected excel and convert to Pandas DataFrame FORTITUDE 2 17,015 Aug-30-2018, 01:08 PM
Last Post: FORTITUDE
  Convert indexing For Loop from MATLAB (uses numpy and pandas) bentaz 3 4,135 Mar-20-2018, 08:29 PM
Last Post: bentaz
  pandas convert date/time to week okl 3 6,627 Mar-03-2018, 10:15 PM
Last Post: marsokod
  How do I convert my data so it works with Pandas? Oliver 0 2,384 Dec-11-2017, 04:09 PM
Last Post: Oliver

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020