相关疑难解决方法(0)

使用dict重新映射pandas列中的值

我有一个字典,看起来像这样: di = {1: "A", 2: "B"}

我想将它应用于数据帧的"col1"列,类似于:

     col1   col2
0       w      a
1       1      2
2       2    NaN
Run Code Online (Sandbox Code Playgroud)

要得到:

     col1   col2
0       w      a
1       A      2
2       B    NaN
Run Code Online (Sandbox Code Playgroud)

我该怎么做才能做到最好?出于某种原因谷歌搜索与此相关的术语只显示了如何从dicts制作列的链接,反之亦然: - /

python dictionary remap pandas

260
推荐指数
6
解决办法
21万
查看次数

如何用字典值替换Pandas系列

我想将我的字典值链接到pandas系列对象。我已经尝试过replace方法和map方法仍然没有运气。

按照链接: 用字典替换pandas系列中的值

仍然无法正常工作,我的示例熊猫看起来像:

index                    column
0                      ESL Literacy
1    Civics  Government Team Sports
2      Health  Wellness Team Sports
3              Literacy Mathematics
4                       Mathematics
Run Code Online (Sandbox Code Playgroud)

字典:

{'civics': 6,
 'esl': 5,
 'government': 7,
 'health': 8,
 'literacy': 1,
 'mathematics': 4,
 'sports': 3,
 'team': 2,
 'wellness': 9}
Run Code Online (Sandbox Code Playgroud)

所需输出:

0 [5,1]
1 [6,7,2,3]
2 [8,9,2,3]
3 [1,4]
4 [4]
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激。谢谢 :)

python dictionary pandas

4
推荐指数
1
解决办法
64
查看次数

标签 统计

dictionary ×2

pandas ×2

python ×2

remap ×1