I've got two data frames that represent similar data but I want to merge after changing the col names. There are a few ways to achieve this but given the size of my actual data frames, I'd like to use the following method. I'm returning nan values for the second df.
import pandas as pd
df1 = pd.DataFrame({
'time': ['2012-08-02 09:50:20.0','2012-08-02 09:50:32.5','2012-08-02 09:50:34.8'],
'Val': ['1,2,3','1,2,3','1,2,3'],
'Val2': [1,2,3],
'Val3': [1.1,2.1,3.1]
})
df2 = pd.DataFrame({
'time': ['2012-08-02 09:50:20.0','2012-08-02 09:50:32.5','2012-08-02 09:50:34.8'],
'Val': ['1,2,3','1,2,3','1,2,3'],
'Val2': [1,2,3],
'Val3': [1.1,2.1,3.1]
})
df1['time'] = pd.to_datetime(df1['time'])
df2['time'] = pd.to_datetime(df2['time'])
df1.columns.values[1:4] = ['first_' + str(x) for x in df1.columns[1:4]]
df2.columns.values[1:4] = ['second_' + str(x) for x in df2.columns[1:4]]
df3 = pd.merge(df1, df2, on = 'time')
print(df3)
time first_Val first_Val2 first_Val3 second_Val second_Val2 second_Val3
0 2012-08-02 09:50:20.000 1,2,3 1 1.1 NaN NaN NaN
1 2012-08-02 09:50:32.500 1,2,3 2 2.1 NaN NaN NaN
2 2012-08-02 09:50:34.800 1,2,3 3 3.1 NaN NaN NaN
Intended output:
time first_Val first_Val2 first_Val3 second_Val second_Val2 second_Val3
0 2012-08-02 09:50:20.000 1,2,3 1 1.1 1,2,3 1 1.1
1 2012-08-02 09:50:32.500 1,2,3 2 2.1 1,2,3 2 2.1
2 2012-08-02 09:50:34.800 1,2,3 3 3.1 1,2,3 3 3.1