【问题标题】:How to combine two dataframes from bank accounts如何组合来自银行账户的两个数据框
【发布时间】:2018-06-13 16:15:00
【问题描述】:

我有两个数据框,其中包含来自两个银行账户的交易。我只想将它们组合成一个数据框。但是,这对我来说效果不佳。数据帧称为dfJLcard,这里有一些信息

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 以避免警告。

标签: python pandas dataframe


【解决方案1】:

你可以试试pd.merge(f,JLcard,left_index=True, right_index=True)

【讨论】:

  • 会按原样复制行还是会尝试以某种方式组合行?我只想将行复制并放入相同的数据框中。
  • 这将基于索引合并两个数据框。但是由于您希望将 JLcard 本质上附加到 df,因此我将首先通过确保它们在拼写和索引顺序上匹配(使用df.sort_index())来标准化列名,然后使用@ALollz 前面提到的pd.concat([df,JLcard])。这将复制行。
猜你喜欢
  • 2015-04-12
  • 1970-01-01
  • 2011-06-10
  • 2014-10-09
  • 1970-01-01
  • 2014-08-27
  • 2017-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多