python - Pandas 从具有不同列名的 2 个数据帧映射数据

标签 python pandas

我尝试映射这两个数据帧,但失败了。也许是因为列名和它的值有点不同。

我想创建一个像 dfNew 一样的新数据框。

df

Employee ID Employee Name   Activity Month
A0001       John Smith      Apr-19
A0002       Will Cornor     Apr-19
A0001       John Smith      May-19
A0003       David Teo       May-19
A0001       John Smith      May-19
A0002       Will Cornor     Jun-19
A0001       John Smith      Jun-19

df2

Month       Bonus
2019-04-01  5000
2019-05-01  4000
2019-06-01  6000

dfNew

Employee ID Employee Name   Activity Month  Bonus
A0001       John Smith      Apr-19          5000
A0002       Will Cornor     Apr-19          5000
A0001       John Smith      May-19          4000
A0003       David Teo       May-19          4000
A0001       John Smith      May-19          4000
A0002       Will Cornor     Jun-19          6000
A0001       John Smith      Jun-19          6000

最佳答案

使用Series.dt.strftime fr 更改日期时间的格式,这样可能Series.map :

s = df2.set_index(df2['Month'].dt.strftime('%b-%y'))['Bonus']
df1['Bonus'] = df1['Activity Month'].map(s)
print (df1)
  Employee     ID Employee Name Activity Month  Bonus
0    A0001   John         Smith         Apr-19   5000
1    A0002   Will        Cornor         Apr-19   5000
2    A0001   John         Smith         May-19   4000
3    A0003  David           Teo         May-19   4000
4    A0001   John         Smith         May-19   4000
5    A0002   Will        Cornor         Jun-19   6000
6    A0001   John         Smith         Jun-19   6000

或者使用DataFrame.mergeDataFrame.pop对于删除原始列的新列:

df2['Activity Month'] = df2.pop('Month').dt.strftime('%b-%y')
df1 = df1.merge(df2, on='Activity Month', how='left')

关于python - Pandas 从具有不同列名的 2 个数据帧映射数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603063/

相关文章:

python - PonyORM、SQLite 性能

python - 使用 BeautifulSoup 编辑 html 中的文本

python - 奇怪的字典迭代顺序

Python 命令行 - 多行输入

python - 无法在 macOS 上的 ipython3 中创建多行语句 block

python - 合并两个 Pandas 数据框多对一

python - 如何对具有非数值的数据框进行分组和透视

python - 如何在多级索引中为某些列(但不是全部)交换级别

python - 每 3 列 Pandas 的平均值

python - 基于 Python 中的列表和字典对多列进行编码