Compare 2 columns of 2 different pandas dataframes, if the same insert 1 into the other in Python

前端 未结 1 1321
迷失自我
迷失自我 2021-01-12 05:00

I have a panda DataFrame with date_time/voltage data like this (df1):

        Date_Time  Chan
0   20130401 9:00   AAT
1  20130401 10:00   AAT
2  20130401 11:         


        
相关标签:
1条回答
  • 2021-01-12 05:29

    You can use a plain ol' merge to do this. But first, you should do a little cleanup of you DataFrames, to make sure your datetime columns are actually datetimes rather than strings (Note: it may be better to do this when reading as csv or whatever):

    df1['Date_Time'] = pd.to_datetime(df1['Date_Time'], format='%Y%m%d %H:%M')
    df2['date_time'] = pd.to_datetime(df2['date_time'])
    

    Let's also rename the Datetime columns with the same name:

    df1.rename(columns={'Date_Time': 'Datetime'}, inplace=True)
    df2.rename(columns={'date_time': 'Datetime'}, inplace=True)
    

    Now a simple merge will give you what you're after:

    In [11]: df1.merge(df2)
    Out[11]: 
                 Datetime Chan  Sens1  Sens2
    0 2013-04-01 09:00:00  AAT  28.82    300
    1 2013-04-01 10:00:00  AAT  28.35   4900
    2 2013-04-01 12:00:00  AAT  28.04    250
    
    In [12]: df1.merge(df2, how='left')
    Out[12]: 
                 Datetime Chan  Sens1  Sens2
    0 2013-04-01 09:00:00  AAT  28.82    300
    1 2013-04-01 10:00:00  AAT  28.35   4900
    2 2013-04-01 11:00:00  AAT    NaN    NaN
    3 2013-04-01 12:00:00  AAT  28.04    250
    4 2013-04-01 13:00:00  AAT    NaN    NaN
    5 2013-04-01 14:00:00  AAT    NaN    NaN
    6 2013-04-01 15:00:00  AAT    NaN    NaN
    
    0 讨论(0)
提交回复
热议问题