Get a running total from a list

前端 未结 4 1683
青春惊慌失措
青春惊慌失措 2021-01-28 02:51

I\'m reading in items:

for line in sys.stdin:
    line = line.strip()
    data = line.split(\"-\")

If I print data as it is read,

4条回答
  •  梦如初夏
    2021-01-28 03:13

    Pandas does a very good job in handling this kind of situations

    import pandas as pd
    df_data=pd.read_csv(filepath_or_buffer=path,sep='_',names =['Name','value'])
    df=df_data.groupby(['Name'])['value'].sum()
    
    print df
    

    output

    'Adam'     13
    'Lucy'      2
    'Peter'    11
    

    Input file

    Adam_5
    Peter_7
    Adam_8
    Lucy_2
    Peter_4
    

提交回复
热议问题