【问题标题】:Adding Dates (Series) column from one DataFrame to the other Pandas, Python将日期(系列)列从一个 DataFrame 添加到另一个 Pandas,Python
【发布时间】:2017-03-07 10:38:49
【问题描述】:

我正在尝试将日期列从 df1“广播”到 df2。

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

df1 and df2 code

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

我已经尝试过 concat() 和 merge() 但它们似乎都不起作用:

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 

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

【问题讨论】:

  • 使用类似pd.concat([df1,df2], axis=1)的东西

标签: python datetime pandas dataframe series


【解决方案1】:

我认为您需要 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 by Series 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

【讨论】:

  • 我认为map() 解决方案是添加单个列时最有效的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-04
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 2021-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多