i have 2 data frames:
df1:
date column1
2014-03-13 1
2014-03-14 2
d2:
date Id people value
2014-03-13 1 A -3.0
2014-03-13 1 B -6.0
2014-03-14 1 A -3.1
2014-03-14 2 B -5.0
and i want to get a df3 =
date Id people value column1
2014-03-13 1 A -3.0 1
2014-03-13 1 B -6.0 1
2014-03-14 1 A -3.1 2
2014-03-14 2 B -5.0 2
i try with the function join of pandas but i get some errors there any way to resolve it
写吧:
df3 = df2.merge(df1, on='date', how='left')
Use only merge
if joined column is only date
in both DataFrames
:
df = pd.merge(df2, df1, how='left')
df['new'] = df2['date'].map(df2.set_index('date')['column1'])
Also check if dtypes
are same in both DataFrame
s:
print (df1['date'].dtype)
print (df2['date'].dtype)
If not, convert column to datetime
, eg:
df1['date'] = pd.to_datetime(df1['date'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.