python - Multiple conditions in selecting pandas dataframe -
i have 2 similar dataframes. df1 , df2 same structure. , have same data in 1 column(policyid) , other columns may have different data , 1 column contains timestamp based on need segregate them. df1 -
policyid,statecode,county,timestamp 114455,fl,clay county,2015-12-23
df2-
policyid,statecode,county,timestamp 114455,fl,clay county,2016-12-34
i need create new dataframe df3 contains data df1 if timestamp of df1 higher , df2 if timestamp of df2 higher.
i think need concat
sort_values
, last need groupby
first
or first
:
print (df1) 0 1 2 3 0 114453 fl clay county 2016-12-23 1 114455 fl1 clay county1 2016-12-11 2 114457 fl1 clay county1 2016-12-11 print (df2) 0 1 2 3 0 114453 fl1 clay county1 2016-12-15 1 114455 fl1 clay county1 2016-12-15 2 114457 fl1 clay county1 2016-12-15 df1.iloc[:,3] = pd.to_datetime(df1.iloc[:,3]) df2.iloc[:,3] = pd.to_datetime(df2.iloc[:,3]) df3 = pd.concat([df1,df2]) df3 = df3.sort_values(df3.columns[3]).groupby(df3.columns[0]).first() print (df3) 1 2 3 0 114453 fl1 clay county1 2016-12-15 114455 fl1 clay county1 2016-12-11 114457 fl1 clay county1 2016-12-11
df3 = pd.concat([df1,df2]) df4 = df3.sort_values(df3.columns[3]).groupby(df3.columns[0]).last() print (df4) 1 2 3 0 114453 fl clay county 2016-12-23 114455 fl1 clay county1 2016-12-15 114457 fl1 clay county1 2016-12-15
Comments
Post a Comment