用熊猫总结不同的列。蟒蛇

时间:2018-01-23 13:32:27

标签: python csv

我有一个代码用于将2个数据帧与pandas连接起来并总计总和。问题是我不想总结'Fecha''hours',因为正如您在示例中看到的那样,我有一行包含所有值,我想在那里有一个空行!

df = pd.read_csv('a_AR.csv')
df1 = pd.read_csv('a_US.csv')

frames = [df1, df]
result = pd.concat(frames)
result = result.sort_values(by=['Fecha','hours'])

del result['eCPM']
del result['Importe_a_pagar_a_medio']

result.loc['Total']= result.sum()

result.to_csv('a_AR-US_Days_hours.csv', index=False)

os.remove('a_US.csv')
os.remove('a_AR.csv')

示例结果:

Fecha,hours,impressions,revenue
22/01/2018,23hs,1666,0.73
22/01/2018,23hs,67,0.02
00hs00hs01hs01hs02hs02hs03hs03...,01/01/201801/01/201801/01/201801/01...,1733,0.75

1 个答案:

答案 0 :(得分:1)

对于没有/home/nao/.local/share/PackageManager/apps/your-package-id/your-dialog-name/your-dialog-name_enu.topFecha列的所有列,您需要difference进行过滤:

hours

另一个更加动态的解决方案select_dtypes用于选择所有cols = df.columns.difference(['Fecha','hours']) 列:

numeric