我有两个数据框df1和df2:df1 = pd.DataFrame({"a" : [1,2,3,4,5,6,7,8,9,10,11,12], "b" : [-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12], "t" : [1,2,3,4,5,6,7,8,9,10,11,12]})df1.index = [1,1,1,2,2,2,3,3,3,4,4,5]df2 = pd.DataFrame({"a" : [10,20,30], "b" : [-10,-20,-30]})df2.index = [2,3,4]它们看起来像这样:df1 a b t1 1 -1 11 2 -2 21 3 -3 32 4 -4 42 5 -5 52 6 -6 63 7 -7 73 8 -8 83 9 -9 94 10 -10 104 11 -11 115 12 -12 12df2 a b2 10 -103 20 -204 30 -30所以 的列df2是的列的子集df1。我想将 的行df1与df2它们具有相同索引的行相乘,并且只保留由它们的索引之间的交集组成的行,即基本上ls_keep = []for i in range(len(df1)): for j in range(len(df2)): if df1.index[i] == df2.index[j]: df1.iloc[i]["a"] = df1.iloc[i]["a"] * df2.iloc[j]["a"] df1.iloc[i]["b"] = df1.iloc[i]["b"] * df2.iloc[j]["b"] ls_keep.append(i)df1 = df1.iloc[ls_keep]这给了我 a b t2 40 40 42 50 50 52 60 60 63 140 140 73 160 160 83 180 180 94 300 300 104 330 330 11此代码按预期工作,但非常不专业且很长,以防只有两列以上。有没有办法使用 的函数来优化它pandas?
1 回答

牛魔王的故事
TA贡献1830条经验 获得超3个赞
首先过滤器仅匹配索引到新DataFrame的boolean indexing和isin,然后通过多mul用df2.columns在这两个处理相同的列DataFrameS:
df11 = df1[df1.index.isin(df2.index)].copy()
df11[df2.columns] = df11[df2.columns].mul(df2)
print (df11)
a b t
2 40 40 4
2 50 50 5
2 60 60 6
3 140 140 7
3 160 160 8
3 180 180 9
4 300 300 10
4 330 330 11
添加回答
举报
0/150
提交
取消