pandas 加入DataFrame强制后缀? [英] pandas join DataFrame force suffix?
本文介绍了 pandas 加入DataFrame强制后缀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在合并或加入时强制使用后缀.我知道如果发生冲突,可以提供一个,但在我的情况下,我将 df1 与 df2 合并,这不会导致任何冲突,但然后在使用后缀的 df2 上再次合并,但我更希望每次合并都有一个后缀,因为如果我像你想象的那样做不同的组合会让人困惑.
How can I force a suffix on a merge or join. I understand it's possible to provide one if there is a collision but in my case I'm merging df1 with df2 which doesn't cause any collision but then merging again on df2 which uses the suffixes but I would prefer for each merge to have a suffix because it gets confusing if I do different combinations as you could imagine.
推荐答案
您可以在实际的 DataFrame 上强制添加后缀:
You could force a suffix on the actual DataFrame:
In [11]: df_a = pd.DataFrame([[1], [2]], columns=['A'])
In [12]: df_b = pd.DataFrame([[3], [4]], columns=['B'])
In [13]: df_a.join(df_b)
Out[13]:
A B
0 1 3
1 2 4
通过附加到它的列名:
In [14]: df_a.columns = df_a.columns.map(lambda x: str(x) + '_a')
In [15]: df_a
Out[15]:
A_a
0 1
1 2
现在连接不需要后缀修正,无论它们是否碰撞:
Now joins won't need the suffix correction, whether they collide or not:
In [16]: df_b.columns = df_b.columns.map(lambda x: str(x) + '_b')
In [17]: df_a.join(df_b)
Out[17]:
A_a B_b
0 1 3
1 2 4
这篇关于 pandas 加入DataFrame强制后缀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文