gpt4 book ai didi

python - 在 pandas 中查找具有不同列名的值

转载 作者:太空宇宙 更新时间:2023-11-03 16:27:00 24 4
gpt4 key购买 nike

我有 2 个 pandas 数据框 df1df2

Name   No
A 1
A 2
B 5

Player Gender
A F
B M
C F

我想使用 df2 中 gender 列的相应值在 df1 数据框中创建一个新列 sex 。用于查找的列是 df1 中的 Namedf2 中的 Player

非常感谢任何帮助

最佳答案

使用map通过 df2 其中 set_index来自玩家列:

df1['sex'] = df1.Name.map(df2.set_index('Player')['Gender'])
print (df1)
Name No sex
0 A 1 F
1 A 2 F
2 B 5 M

这与 dictmap 相同:

d = df2.set_index('Player')['Gender'].to_dict()
print (d)
{'A': 'F', 'B': 'M', 'C': 'F'}
df1['sex'] = df1.Name.map(d)
print (df1)
Name No sex
0 A 1 F
1 A 2 F
2 B 5 M

或者:

print (pd.merge(df1,df2, left_on='Name', right_on='Player')
.rename(columns={'Gender':'sex'})
.drop('Player', axis=1))

Name No sex
0 A 1 F
1 A 2 F
2 B 5 M

第一个更快:

In [46]: %timeit (pd.merge(df1,df2, left_on='Name', right_on='Player').rename(columns={'Gender':'sex'}).drop('Player', axis=1))
The slowest run took 4.53 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 2.53 ms per loop

In [47]: %timeit df1.Name.map(df2.set_index('Player')['Gender'])
The slowest run took 4.78 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 882 µs per loop

关于python - 在 pandas 中查找具有不同列名的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37894807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com