0
votes

Could you please support me to clarify the proper way to convert a selected Dataframe column into string ?

'Product_ID' part of dataframe 'df' is automatically selected as integer

If I use following statement:

df['Product_ID']=df['Product_ID'].to_string()

generate error:

TypeError: Can't convert 'int' object to str implicitly

same issue is generated by .astype(str) or .apply(str)

thanks!

1

1 Answers

1
votes

First, note that to_string and .astype(str) do different things. to_string returns a single string, and .astype(str) returns a series with string values. Which are you trying to do?

Second, how sure are you that you are working with an integer series? What does df['Product_ID'].dtype return?

Third, can you try to post a reproducible example? One way to narrow down the data that is causing the problem:

for i,v in enumerate(df['Product_ID'].values):
    try:
        str(v)
    except TypeError:
        print i, v