数据和分布的日期-时间序列分组

2024-04-25 15:29:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将日期时间序列与存储库数据合并,同时按名称分组并求和值。你知道吗

File1.csv 

Timeseries,Name,count
07/03/2015 06:00:00,Paris,100
07/03/2015 06:00:00,Paris,600
07/03/2015 06:00:00,Paris,700
07/03/2015 06:00:00,London,200
07/03/2015 06:00:00,London,100
07/03/2015 06:00:00,London,500
07/03/2015 06:00:00,Dublin,300
07/03/2015 06:00:00,Dublin,400
07/03/2015 06:00:00,Dublin,400

输出

Master_file.csv (append mode)

    Name,Timeseries(n-1)Timeseries(n)#put the datetime series as header and put       
    Paris,300,1400      #Sum of all the values with same Name
    London,200,800
    Dublin,400,1100

Program 

import pandas as pd 
import numpy as np

df = pd.read_csv('/home/lat_lon1.csv')
df1 = pd.read_csv('/home/lat_lon_master.csv')


gp = df.groupby('Name')['date timeseries'].sum().reset_index() 
df1.merge(gp, on='Name')

我很难将date time列更改为header并将正确的值放在下面。未找到的Names可以给出NAN并在下一次迭代中替换。你知道吗


Tags: csvthenameimportdfhomereadput
1条回答
网友
1楼 · 发布于 2024-04-25 15:29:26

请检查python数据框文档 Click here 这是您正在查看的代码。你知道吗

Output

Timeseries Name count 07/03/2015 06:00:00 Dublin 1100 07/03/2015 06:00:00 London 800 07/03/2015 06:00:00 Paris 1400

   #!/bin/python
    import pandas as pd
    import numpy as np
    df=pd.read_csv('/home/saiharsh/Documents/Crowd Street/Transition_Data/Telecom_7.csv') #Please enter the file Location
    gp=df.groupby('Name').sum().reset_index()
    flag=0
    for i in gp['Name']:
        if flag==1:
            time=df['Timeseries'][df['Name']==i]
            time=time.tail(1)
            frames=[time1,time]
            time1=pd.concat(frames)
        else:
            time1=df['Timeseries'][df['Name']==i]
            time1=time1.tail(1)
            flag=1
    time1=time1.reset_index(drop=True)
    result=pd.concat([time1,gp],axis=1,join='inner')
    result=result.to_csv(index=False)
    print result

Please feel free to reply if any problem.

相关问题 更多 >