将日期(系列)列从一个DataFrame添加到其他Pandas,Python [英] Adding Dates (Series) column from one DataFrame to the other Pandas, Python
问题描述
我正在尝试从df1到df2广播一个日期列。
I am trying to 'broadcast' a date column from df1 to df2.
在df1中,我有所有用户的名字及其基本信息。
在df2我有一个由用户进行的购买清单。
In df1 I have the names of all the users and their basic information. In df2 I have a list of purchases made by the users.
假设我有一个更大的数据集(以上为样本创建)我可以添加(!)df1 ['DoB']列到df2?
我尝试过concat()和merge()但没有他们似乎工作:
I have tried both concat() and merge() but none of them seem to work:
只有将df1和df2合并在一起,然后才删除不需要的列,唯一的方法似乎是工作。但是如果我有几十个不需要的列,这将是非常有问题的。
The only way it seems to work is only if I merge both df1 and df2 together and then just delete the columns I don't need. But if I have tens of unwanted columns, it is going to be very problematic.
完整的代码(包括引发错误的行):
The full code (including the lines that throw an error):
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
任何帮助将不胜感激。谢谢:)
Any help would be much appreciated. Thank you :)
推荐答案
我想你需要 合并
与子集 [['Name','DoB']]
- 需要code>名称列匹配:
I think you need merge
with subset [['Name','DoB']]
- need Name
column for matching:
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
这篇关于将日期(系列)列从一个DataFrame添加到其他Pandas,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!