如何映射 Pandas 系列中的字符串
How to map strings in a Pandas Series
有没有办法 map
characters
for a column
in Pandas
.
例如,我想像这样映射一个列 info
{"1": "US", "2":"DE", "3":"CA", "4":"AU", "5":"BE"}
所以我想要一些 US
和其余的来显示
而不是数字
编辑注意:我不想分解列,我想保持原样,只用字符串替换数字
0 ['3']
1 ['6']
2 ['3','4']
3 ['3','4','6']
4 ['3','4']
5 ['6']
6 ['6']
7 ['5']
8 ['5']
9 ['3', '4', '1']
看起来你可以这样做:
d = {"1": "US", "2":"DE", "3":"CA", "4":"AU", "5":"BE"}
df.dropna().explode('my_col').my_col.map(d).groupby(level=0).agg(list).reindex(df.index)
如果你不想爆炸,请使用申请。但请记住,在大型数据帧上,explode 方法可能比这快得多
测试数据帧
>>> df
test
0 [3, 4]
1 [6]
2 [3]
3 [3, 4, 6]
映射字典
>>> info = {"1": "US", "2":"DE", "3":"CA", "4":"AU", "5":"BE"}
代码
>>> df.test.apply(lambda x: [info.get(str(i)) for i in x])
0 [CA, AU]
1 [None]
2 [CA]
3 [CA, AU, None]
Name: test, dtype: object
有没有办法 map
characters
for a column
in Pandas
.
例如,我想像这样映射一个列 info
{"1": "US", "2":"DE", "3":"CA", "4":"AU", "5":"BE"}
所以我想要一些 US
和其余的来显示
编辑注意:我不想分解列,我想保持原样,只用字符串替换数字
0 ['3']
1 ['6']
2 ['3','4']
3 ['3','4','6']
4 ['3','4']
5 ['6']
6 ['6']
7 ['5']
8 ['5']
9 ['3', '4', '1']
看起来你可以这样做:
d = {"1": "US", "2":"DE", "3":"CA", "4":"AU", "5":"BE"}
df.dropna().explode('my_col').my_col.map(d).groupby(level=0).agg(list).reindex(df.index)
如果你不想爆炸,请使用申请。但请记住,在大型数据帧上,explode 方法可能比这快得多
测试数据帧
>>> df
test
0 [3, 4]
1 [6]
2 [3]
3 [3, 4, 6]
映射字典
>>> info = {"1": "US", "2":"DE", "3":"CA", "4":"AU", "5":"BE"}
代码
>>> df.test.apply(lambda x: [info.get(str(i)) for i in x])
0 [CA, AU]
1 [None]
2 [CA]
3 [CA, AU, None]
Name: test, dtype: object