将日期(系列)列从一个 DataFrame 添加到另一个 Pandas、Python

Adding Dates (Series) column from one DataFrame to the other Pandas, Python

我正在尝试 'broadcast' 从 df1 到 df2 的日期列。

在 df1 中我有所有用户的名字和他们的基本信息。 在 df2 中,我有一份用户购买的清单。

df1 and df2 code

假设我有一个更大的数据集(上面为示例创建的数据集)我如何才能将 df1['DoB'] 列添加到 df2?

我已经尝试了 concat() 和 merge() 但其中 none 似乎有效:

code and error

它似乎唯一可行的方法是将 df1 和 df2 合并在一起,然后只删除我不需要的列。但是,如果我有数十个不需要的列,那将是非常有问题的。

完整代码(包括抛出错误的行):

import pandas as pd
df1 = pd.DataFrame(columns=['Name','Age','DoB','HomeTown'])

df1['Name'] = ['John', 'Jack', 'Wendy','Paul']
df1['Age'] = [25,23,30,31]
df1['DoB'] = pd.to_datetime(['04-01-2012', '03-02-1991', '04-10-1986', '06-03-1985'], dayfirst=True)
df1['HomeTown'] = ['London', 'Brighton', 'Manchester', 'Jersey']

df2 = pd.DataFrame(columns=['Name','Purchase'])
df2['Name'] = ['John','Wendy','John','Jack','Wendy','Jack','John','John']
df2['Purchase'] = ['fridge','coffee','washingmachine','tickets','iPhone','stove','notebook','laptop']

df2 = df2.concat(df1) # error

df2 = df2.merge(df1['DoB'], on='Name', how='left') #error

df2 = df2.merge(df1, on='Name', how='left')
del df2['Age'], df2['HomeTown']
df2 #that's how i want it to look like 

如有任何帮助,我们将不胜感激。谢谢:)

我认为您需要 merge 和子集 [['Name','DoB']] - 需要 Name 列进行匹配:

print (df1[['Name','DoB']])
    Name        DoB
0   John 2012-01-04
1   Jack 1991-02-03
2  Wendy 1986-10-04
3   Paul 1985-03-06

df2 = df2.merge(df1[['Name','DoB']], on='Name', how='left')
print (df2)
    Name        Purchase        DoB
0   John          fridge 2012-01-04
1  Wendy          coffee 1986-10-04
2   John  washingmachine 2012-01-04
3   Jack         tickets 1991-02-03
4  Wendy          iPhone 1986-10-04
5   Jack           stove 1991-02-03
6   John        notebook 2012-01-04
7   John          laptop 2012-01-04

map 系列 s 的另一个解决方案:

s = df1.set_index('Name')['DoB']
print (s)
Name
John    2012-01-04
Jack    1991-02-03
Wendy   1986-10-04
Paul    1985-03-06
Name: DoB, dtype: datetime64[ns]

df2['DoB'] = df2.Name.map(s)
print (df2)
    Name        Purchase        DoB
0   John          fridge 2012-01-04
1  Wendy          coffee 1986-10-04
2   John  washingmachine 2012-01-04
3   Jack         tickets 1991-02-03
4  Wendy          iPhone 1986-10-04
5   Jack           stove 1991-02-03
6   John        notebook 2012-01-04
7   John          laptop 2012-01-04