pandas 级数的逐元素运算
Elementwise operation on pandas series
我有一个 pandas 系列 x
,其值为 1
、2
或 3
。
我希望它具有值 monkey
、gorilla
和 tarzan
,具体取决于这些值。
我想我应该做类似的事情
values = ['monkey', 'gorilla', 'tarzan']
x = values[x - 1]
但它不起作用。我猜是因为它没有按元素操作。
通过 dict
和函数 map
使用映射。
样本:
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
我有一个 pandas 系列 x
,其值为 1
、2
或 3
。
我希望它具有值 monkey
、gorilla
和 tarzan
,具体取决于这些值。
我想我应该做类似的事情
values = ['monkey', 'gorilla', 'tarzan']
x = values[x - 1]
但它不起作用。我猜是因为它没有按元素操作。
通过 dict
和函数 map
使用映射。
样本:
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