计算熊猫的每月总支出

时间:2018-08-19 04:19:11

标签: python pandas

我有一个带有费用清单的文件:

示例:

date        item                        out     in
12/01/2017  PAGO FIBERTEL               668.5   0.0
12/01/2017  PAGO GAS NATURAL            2.32    0.0
10/02/2017  EXTRACCION TARJETA          1200.0  0.0
10/02/2017  CPA. STARBUCKS R. PE9A      105.0   0.0
10/02/2017  CPA. STARBUCKS R. PE9A      125.0   0.0
11/03/2017  EXTRACCION TARJETA          1200.0  0.0
11/03/2017  SALES                       0.0     10000.0

我想制作一个绘图,在其中可以查看每年一年中某些项目的演变情况。例如,我将使用“ startbucks”作为关键字过滤“ item”列,我将计算每月汇总,并显示如下信息:

             Dec   Jan Mar
Starbucks    0     0   230 

我从json文件中提取了一系列关键字,这些关键字将用于产生每一行。但是,我不能只用一个。我已经尝试了多种形式的groupby(使用grouper和不使用grouper),但是我认为我没有。这是我目前获得的代码:

import pandas as pd
import matplotlib.pyplot as plt
import sys
import json

class Banca():
def __init__(self, name, csv_path, json_path):
    self.name= name
    self.df = pd.read_csv(csv_path)
    with open(json_path) as j:
        self.json = json.load(j)

def prepare(self):
    #Add header
    headers = ['fecha','concepto','in','out',"x"]
    self.df.columns = headers

    #fix data
    self.df.fecha = pd.to_datetime(self.df.fecha)


    #Here i'm testing, this doesnt work
    g1=self.df.groupby(pd.Grouper(key='fecha', freq='M')['in'].sum())
    print(g1.describe().to_string())
    print(g1.head())

    #g1.plot(y='out', style='.-', figsize=(15,4))
    #plt.show()
    #filter data
    # some filter

def grafica(self):
    #plot data
    self.df.plot(x='fecha', y='out',style='.-', figsize=(15,4))
    plt.show()

def test_df(self):
    print(self.df.describe(include='all'))

def test_json(self):
    for x,y in self.json.items():
        print(x,y)



icbc = Banca("ICBC", sys.argv[1], sys.argv[2])
icbc.test_df()
icbc.prepare()
#icbc.grafica()
#icbc.test_json()

我正在编写此代码,作为学习熊猫操作数据的练习。我已经学到了很多关节,但是我已经在这里停留了一段时间。我在想也许我不应该为此使用groupby,而是其他。无论如何,我感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

使用:

#convert column to datetimes if necessary
df['fecha'] = pd.to_datetime(df['fecha'], format='%d/%m/%Y')
print(df)
       fecha                concepto       in      out
0 2017-01-12           PAGO FIBERTEL   668.50      0.0
1 2017-01-12        PAGO GAS NATURAL     2.32      0.0
2 2017-02-10      EXTRACCION TARJETA  1200.00      0.0
3 2017-02-10  CPA. STARBUCKS R. PE9A   105.00      0.0
4 2017-02-10  CPA. STARBUCKS R. PE9A   125.00      0.0
5 2017-03-11      EXTRACCION TARJETA  1200.00      0.0
6 2017-03-11                   SALES     0.00  10000.0

import re

#create DatetimeIndex
df = df.set_index('fecha')

#list of values
L = ['starbuck','pago']
all_s = []
for x in L:
    #filter by substrings, select column in
    s = df.loc[df['concepto'].str.contains(x, flags=re.I), 'in']
    #aggregate by months and sum
    s = s.groupby(pd.Grouper(freq='M')).sum()
    #change format of index by `MM-YYYY`
    s.index = s.index.strftime('%b-%Y')
    all_s.append(s.rename(x))

#join all Series together and transpose 
df = pd.concat(all_s, axis=1).T
print (df)
          Feb-2017  Jan-2017
starbuck     230.0       NaN
pago           NaN    670.82

编辑:

对于绘图,最好绘制DatetimeIndex和按关键字划分的列,还可以对月份的开始按MS分组,如果要添加由0填充的缺失月份,则可以添加asfreq。 :

df['fecha'] = pd.to_datetime(df['fecha'], format='%d/%m/%Y')
print(df)
       fecha                concepto       in      out
0 2017-01-12           PAGO FIBERTEL   668.50      0.0
1 2017-01-12        PAGO GAS NATURAL     2.32      0.0
2 2017-02-10      EXTRACCION TARJETA  1200.00      0.0
3 2017-02-10  CPA. STARBUCKS R. PE9A   105.00      0.0
4 2017-02-10  CPA. STARBUCKS R. PE9A   125.00      0.0
5 2017-03-11      EXTRACCION TARJETA  1200.00      0.0
6 2017-05-11                   SALES    20.00  10000.0 <-changed last month

import re

df = df.set_index('fecha')

L = ['starbuck','pago', 'sales']
all_s = []
for x in L:
    s = df.loc[df['concepto'].str.contains(x, flags=re.I), 'in']
    s = s.groupby(pd.Grouper(freq='MS')).sum()
    all_s.append(s.rename(x))

df = pd.concat(all_s, axis=1).fillna(0).asfreq('MS', fill_value=0)
print (df)
            starbuck    pago  sales
fecha                              
2017-01-01       0.0  670.82    0.0
2017-02-01     230.0    0.00    0.0
2017-03-01       0.0    0.00    0.0
2017-04-01       0.0    0.00    0.0
2017-05-01       0.0    0.00   20.0

df.plot(style='.-', figsize=(15,4))