我有两个dataframes这样的:高效串联忽略后缀
import pandas as pd
df1 = pd.DataFrame({"c1": range(5), "c2": range(1, 6)}, index=list("ABCDE"))
df2 = pd.DataFrame({"c3": range(15, 21), "c4": range(11, 17)}, index=["A_suf1", "B_suf2", "A_suf2", "C_suf2", "B_suf1", "D_suf1"]) c1 c2
A 0 1
B 1 2
C 2 3
D 3 4
E 4 5
c3 c4
A_suf1 15 11
B_suf2 16 12
A_suf2 17 13
C_suf2 18 14
B_suf1 19 15
D_suf1 20 16
,我想变成
c3 c4 c1
A_suf1 15 11 0
B_suf2 16 12 1
A_suf2 17 13 0
C_suf2 18 14 2
B_suf1 19 15 1
D_suf1 20 16 3
所以,我想连接独立后缀suf1
的dataframes df2
的索引中的suf2
。由于c1
在df1
中的条目对于列A
为0,因此它应该出现在连接的数据帧中用于条目A_suf1
和A_suf2
。
我当前实现此如下:
# store original name of index
old_index = df2.index
# temporary column which creates values which are in the the index of df1
df2['helper'] = df2.reset_index()["index"].apply(lambda x: x.split("_")[0]).tolist()
# prepare concat
df2 = df2.set_index("helper")
# concat
df_final = pd.concat([df2, df1.loc[:, "c1"]], join="inner", axis=1)
# reset index to original values
df_final.index = old_index
这给了我所需的输出。
但是,它需要一个相当慢的apply
,如果df2
中有一个索引条目不在df1
中,它也会失败。例如,上面的代码将用于df2
失败等于因此
df2 = pd.DataFrame({"c3": range(15, 22), "c4": range(11, 18)}, index=["A_suf1", "B_suf2", "A_suf2", "C_suf2", "B_suf1", "D_suf1", "F_suf1"])
c3 c4
A_suf1 15 11
B_suf2 16 12
A_suf2 17 13
C_suf2 18 14
B_suf1 19 15
D_suf1 20 16
F_suf1 21 17
问题是是否存在在那里更有效和更通用的解决方案的工作,并且还分别非工作的情况下,。
非常漂亮(upvoted)!似乎有效地解决了我的两个问题。 – Cleb
@cᴏʟᴅsᴘᴇᴇᴅ - 是的,这是另一种解决方案。祝你好运! – jezrael