Python 如何组合来自银行帐户的两个数据帧
我有两个数据框,其中包含来自两个银行账户的交易。我只想将它们组合成一个数据帧。然而,这对我来说并不奏效。数据帧被称为Python 如何组合来自银行帐户的两个数据帧,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据框,其中包含来自两个银行账户的交易。我只想将它们组合成一个数据帧。然而,这对我来说并不奏效。数据帧被称为df和JLcard,下面是一些信息 df.shape (1405, 3) JLcard.shape (96, 3) df.columns Index([u'Transaction_Type', u'Transaction_Description', u'transaction'], dtype='object') JLcard.columns Index([u'Transac
df
和JLcard
,下面是一些信息
df.shape
(1405, 3)
JLcard.shape
(96, 3)
df.columns
Index([u'Transaction_Type', u'Transaction_Description', u'transaction'], dtype='object')
JLcard.columns
Index([u'Transaction_Description', u'transaction', u'Transaction_Type'], dtype='object')
因此,这两个数据帧具有相同的列名(如果顺序不同)
也有按日期编制的索引
df.head(3)
Transaction_Type Transaction_Description transaction
date
2017-05-26 BGC UNIV 2997.71
2017-05-30 FPO PT -2650.00
2017-05-30 SO NS 664.00
JLcard.head(3)
Transaction_Description transaction Transaction_Type
date
2017-12-11 MW 128.23 Js card
2017-12-12 WW 179.47 Js card
2017-12-13 XW 42.00 Js card
为了将它们组合到一个数据帧中,我尝试了pd.concat([df,JLcard])
,这给了我:
FutureWarning: Sorting because non-concatenation axis is not aligned. A future version
of pandas will change to not sort by default.
To accept the future behavior, pass 'sort=True'.
To retain the current behavior and silence the warning, pass sort=False
"""Entry point for launching an IPython kernel.
结果数据帧也不按索引排序。例如
Transaction_Description Transaction_Type transaction
date
2018-04-10 ES DEB -16.57
2018-04-04 OR Js card 109.30
2018-04-05 WR Js card 125.00
为什么说“非连接轴未对齐”?为什么
它说它正在排序,而它似乎不是?我该怎么办
为了避免警告?我只想从一行复制所有行
然后按索引(即日期)排序
您可以尝试合并(f,JLcard,left_index=True,right_index=True)
axis=1
通过添加新列来连接两个数据帧。由于它们具有相同的列标题,我认为您只需要pd.concat([df,JLcard])
添加新的rows@ALollz这给了我“FutureWarning:排序,因为非连接轴未对齐。熊猫的未来版本将默认更改为“不排序”。要接受将来的行为,请传递“sort=True”。要保留当前行为并使警告静音,请传递sort=False“”“启动IPython内核的入口点”。@ALollz,然后数据帧不按索引排序。(我编辑了问题。谢谢)你也可以对索引排序df=df.sort\u index()
。这和连接一起起作用吗?不确定排序警告,可能在concat中设置sort=True
,以避免出现警告。这是按原样复制行还是尝试以某种方式合并行?我只想把这些行复制到同一个数据帧中。这将基于索引合并两个数据帧。但是,由于您希望将JLcard本质上附加到df中,因此我将首先标准化列名,确保它们在拼写和索引顺序上匹配(使用df.sort_index()
),然后使用pd.concat([df,JLcard])
,正如@ALollz前面提到的那样。这将复制行。