【发布时间】:2018-06-13 16:15:00
【问题描述】:
我有两个数据框,其中包含来自两个银行账户的交易。我只想将它们组合成一个数据框。但是,这对我来说效果不佳。数据帧称为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
为什么会说“非串联轴不对齐”?为什么 它说它在排序时似乎不是?我能做什么 避免警告?我只是想从一个复制所有行 进入另一个并按索引(即日期)排序。
【问题讨论】:
-
axis = 1看起来通过添加新列来连接两个数据框。由于它们具有相同的列标题,我认为您只希望pd.concat([df,JLcard])添加新行 -
@ALollz 这给了我“FutureWarning:排序,因为非连接轴未对齐。未来版本的熊猫将默认更改为不排序。要接受未来的行为,请传递 'sort=True '。要保留当前行为并使警告静音,请传递 sort=False """启动 IPython 内核的入口点。"
-
@ALollz 然后数据框不按索引排序。 (我编辑了问题。谢谢)
-
您也可以对索引进行排序。
df = df.sort_index()。这与串联工作相结合吗?不确定排序警告,也许在 concat 中设置sort=True以避免警告。