Python 熊猫系列的元素操作
我有一个熊猫系列Python 熊猫系列的元素操作,python,pandas,sklearn-pandas,Python,Pandas,Sklearn Pandas,我有一个熊猫系列x,其值为1、2或3 我希望它有值猴子,大猩猩,和泰山,具体取决于这些值 我想我应该做些类似的事情 values = ['monkey', 'gorilla', 'tarzan'] x = values[x - 1] 但它不起作用。我想这是因为它的操作不合理。使用带有函数的映射方式dict 样本: s = pd.Series([1,2,3]) print (s) 0 1 1 2 2 3 dtype: int64 d = {1:'monkey',2:'g
x
,其值为1
、2
或3
我希望它有值猴子
,大猩猩
,和泰山
,具体取决于这些值
我想我应该做些类似的事情
values = ['monkey', 'gorilla', 'tarzan']
x = values[x - 1]
但它不起作用。我想这是因为它的操作不合理。使用带有函数的映射方式
dict
样本:
s = pd.Series([1,2,3])
print (s)
0 1
1 2
2 3
dtype: int64
d = {1:'monkey',2:'gorilla',3:'tarzan'}
print (s.map(d))
0 monkey
1 gorilla
2 tarzan
dtype: object
通过
dict
和函数使用映射
样本:
s = pd.Series([1,2,3])
print (s)
0 1
1 2
2 3
dtype: int64
d = {1:'monkey',2:'gorilla',3:'tarzan'}
print (s.map(d))
0 monkey
1 gorilla
2 tarzan
dtype: object