-
HUWWW
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 interval0 ch1 2019-10-12 00:00:00 1 601 ch1 2019-10-12 00:01:00 2 602 ch2 2019-10-12 00:02:00 3 603 ch3 2019-10-12 00:03:00 4 60df2 ch_name time value interval0 ch3 2019-10-11 00:00:00 10 701 ch1 2019-10-11 00:01:00 20 702 ch5 2019-10-11 00:02:00 30 703 ch3 2019-10-11 00:03:00 40 70Create 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 interval0 ch1 2019-10-12 00:00:00 1 601 ch1 2019-10-12 00:01:00 2 603 ch3 2019-10-12 00:03:00 4 60new_df3=df2[df2.ch_name.isin(ss)] ch_name time value interval0 ch3 2019-10-11 00:00:00 10 701 ch1 2019-10-11 00:01:00 20 703 ch3 2019-10-11 00:03:00 40 70
-
慕的地10843
您可以使用设置逻辑执行此操作:df = pd.DataFrame(columns = ['a1', 'ba1', 'b1', 'c1'])df1 = pd.DataFrame(columns = ['a1', 'gg', 'b1', 'c2'])df a1 ba1 b1 c1df1 a1 gg b1 c2要从 df 中删除 df1 中的列:df.drop(columns = set(df.columns) - set(df1.columns), inplace=True)df a1 b1
-
芜湖不芜
你试过合并吗?我创建了一些虚假数据来向您展示它是如何工作的。 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 是你定义要匹配的列的方式。我希望它有所帮助。