简体   繁体   中英

Format the values of a dataframe column from integer to string

Currently I have a dataframe with columns but for this purpose, I'll just list the columns of importance. Basically I have a dataframe like this:

id  type
1   1
2   1
3   2
4   2
5   1

But I want to format the integers such that

1 = 'One'
2 = 'Two'

But I don't want to change the int to a string like using pd.replace, but format it instead.

this is random but I've been trying to contact you via KVR Audio but I'm not a member for long enough there. Just wondering how I can contact you?

you can actually use replace function on pandas. For example,

df['type'].replace(1, 'one')

then, you can try replace all the keyword such as for two, three, and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM