Python 不导出所有列
我刚接触熊猫,我用谷歌搜索了我的问题,但没有得到任何帮助 问题陈述: 在对一列Python 不导出所有列,python,pandas,Python,Pandas,我刚接触熊猫,我用谷歌搜索了我的问题,但没有得到任何帮助 问题陈述: 在对一列amount执行cumsum()后,使用df.to_CSV()保存最终CSV时,我的CSV仅显示一列amount,但我希望最终CSV中的所有列 样本数据: *------------------------------------------------* |effective_date | account_id | currency | amount | *------------------------------
amount
执行cumsum()
后,使用df.to_CSV()
保存最终CSV时,我的CSV仅显示一列amount
,但我希望最终CSV中的所有列
样本数据:
*------------------------------------------------*
|effective_date | account_id | currency | amount |
*------------------------------------------------*
| 12/26/19 1 USD 50 |
| 12/27/19 1 USD 70 |
| 11/06/19 2 USD 90 |
| 11/07/19 2 USD 30 |
*------------------------------------------------*
*------------------------------------------------*
|effective_date | account_id | currency | amount |
*------------------------------------------------*
| 12/26/19 1 USD 50 |
| 12/27/19 1 USD 120 |
| 11/06/19 2 USD 90 |
| 11/07/19 2 USD 120 |
*------------------------------------------------*
使用Jupyter笔记本的我的代码:
import pandas as pd
df = pd.read_csv('payments.csv', index_col=0)
df['effective_when'] = pd.to_datetime(df['effective_when'])
df = df.groupby(['account_id', 'currency', 'effective_date']).sum().groupby(level=[0]).cumsum()
df.to_csv ('cumulativePayments.csv')
当前结果:
*------*
|amount|
*------*
| 50 |
| 120 |
| 90 |
| 120 |
*------*
预期结果:
*------------------------------------------------*
|effective_date | account_id | currency | amount |
*------------------------------------------------*
| 12/26/19 1 USD 50 |
| 12/27/19 1 USD 70 |
| 11/06/19 2 USD 90 |
| 11/07/19 2 USD 30 |
*------------------------------------------------*
*------------------------------------------------*
|effective_date | account_id | currency | amount |
*------------------------------------------------*
| 12/26/19 1 USD 50 |
| 12/27/19 1 USD 120 |
| 11/06/19 2 USD 90 |
| 11/07/19 2 USD 120 |
*------------------------------------------------*
如何实现这一点?我认为您可以拆分代码,创建带有聚合的
数据框
,然后重新分配金额
列,这些列由累积金额填充:
df = pd.read_csv('payments.csv', index_col=0)
df['effective_date'] = pd.to_datetime(df['effective_date'])
df = df.groupby(['account_id', 'currency', 'effective_date'], as_index=False).sum()
df['amount'] = df.groupby('account_id')['amount'].cumsum()
print (df)
account_id currency effective_date amount
0 1 USD 2019-12-26 50
1 1 USD 2019-12-27 120
2 2 USD 2019-11-06 90
3 2 USD 2019-11-07 120
df.to_csv('cumulativePayments.csv')
另一个想法是将第一列转换为datetimes,并为写入文件删除默认索引值:
df = pd.read_csv('payments.csv', parse_dates=[0])
df = df.groupby(['account_id', 'currency', 'effective_date'], as_index=False).sum()
df['amount'] = df.groupby('account_id')['amount'].cumsum()
df.to_csv('cumulativePayments.csv', index=False)