3 回答
TA贡献1874条经验 获得超12个赞
Test data:
df1=pd.DataFrame({"ch_name":["ch1","ch1","ch2","ch3"],"time":pd.date_range("2019-10-12",freq="min",periods=4),"value":[1,2,3,4],"interval":[60,60,60,60]})
df2=pd.DataFrame({"ch_name":["ch3","ch1","ch5","ch3"],"time":pd.date_range("2019-10-11",freq="min",periods=4),"value":[10,20,30,40],"interval":[70,70,70,70]})
df1
ch_name time value interval
0 ch1 2019-10-12 00:00:00 1 60
1 ch1 2019-10-12 00:01:00 2 60
2 ch2 2019-10-12 00:02:00 3 60
3 ch3 2019-10-12 00:03:00 4 60
df2
ch_name time value interval
0 ch3 2019-10-11 00:00:00 10 70
1 ch1 2019-10-11 00:01:00 20 70
2 ch5 2019-10-11 00:02:00 30 70
3 ch3 2019-10-11 00:03:00 40 70
Create a set/array for the common column names:
#ss=set(df1.ch_name) & set(df2.ch_name)
ss= np.intersect1d(df1.ch_name,df2.ch_name)
Create the new data frames:
new_df1=df1[df1.ch_name.isin(ss)]
ch_name time value interval
0 ch1 2019-10-12 00:00:00 1 60
1 ch1 2019-10-12 00:01:00 2 60
3 ch3 2019-10-12 00:03:00 4 60
new_df3=df2[df2.ch_name.isin(ss)]
ch_name time value interval
0 ch3 2019-10-11 00:00:00 10 70
1 ch1 2019-10-11 00:01:00 20 70
3 ch3 2019-10-11 00:03:00 40 70
TA贡献1785条经验 获得超8个赞
您可以使用设置逻辑执行此操作:
df = pd.DataFrame(columns = ['a1', 'ba1', 'b1', 'c1'])
df1 = pd.DataFrame(columns = ['a1', 'gg', 'b1', 'c2'])
df
a1 ba1 b1 c1
df1
a1 gg b1 c2
要从 df 中删除 df1 中的列:
df.drop(columns = set(df.columns) - set(df1.columns), inplace=True)
df
a1 b1
TA贡献1796条经验 获得超7个赞
你试过合并吗?
我创建了一些虚假数据来向您展示它是如何工作的。
data1 = [['ch1','12:55',512],['ch2','12:57',516], ['ch3','12:57',420],
['ch4','11:48',433],['ch5','10:42',467]]
data2 = [['ch1','12:55',511],['ch2','12:54',519],['ch3','12:51',720],
['ch5','10:45',497]]
import pandas as pd
df1 = pd.DataFrame(data1,columns=('ch_name','time','value'))
df2 = pd.DataFrame(data2,columns=('ch_name','time','value'))
dfResult = pd.merge(df1, df2, on='ch_name', how='inner')
print(dfResult)
ch_name time_x value_x time_y value_y
0 ch1 12:55 512 12:55 511
1 ch2 12:57 516 12:54 519
2 ch3 12:57 420 12:51 720
3 ch5 10:42 467 10:45 497
这里的关键是参数 how='inner' 和 on='ch_name'。how='inner' 是你说你想要出现在两个数据框中的列的方式,而 on = column_name 是你定义要匹配的列的方式。
我希望它有所帮助。
添加回答
举报