答案 0 :(得分:2)
您可以在创建map
之后使用dict
,但让我们尝试一些新的东西
pd.cut(df.Seasons,4,labels=['Winter','Spring','Summer','Fall'])
Out[262]:
0 Winter
1 Spring
2 Summer
3 Fall
4 Summer
dtype: category
Categories (4, object): [Winter < Spring < Summer < Fall]
确定使用地图
d=dict(zip([1,2,3,4],['Winter', 'Spring', 'Summer', 'Fall']))
df.Seasons.map(d)
Out[265]:
0 Winter
1 Spring
2 Summer
3 Fall
4 Summer
dtype: object
答案 1 :(得分:0)
一种解决方案:
data.csv
Age,Population,Seasons
20,100,1
30,340,2
35,45,3
40,90,4
45,9,3
test.py
#!/bin/python
import pandas as pd
df = pd.read_csv('data.csv')
seasons = ['Winter', 'Spring', 'Summer', 'Fall']
df['Seas_Fact'] = [seasons[x-1] for x in df['Seasons']]
print(df)
输出:
Age Population Seasons Seas_Fact
0 20 100 1 Winter
1 30 340 2 Spring
2 35 45 3 Summer
3 40 90 4 Fall
4 45 9 3 Summer