简体   繁体   English

操纵纬度和时间序列的熊猫

[英]Manipulations with Lat-Lon and Time Series Pandas

I am trying to do some file merging with Latitude and Longitude. 我正在尝试将某些文件与纬度和经度合并。

Input File1.csv

Name,Lat,Lon,timeseries(n)
London,80.5234,121.0452,523
London,80.5234,121.0452,732
London,80.5234,121.0452,848
Paris,90.4414,130.0252,464
Paris,90.4414,130.0252,829
Paris,90.4414,130.0252,98
New York,110.5324,90.0023,572
New York,110.5324,90.0023,689
New York,110.5324,90.0023,794


File2.csv
Name,lat,lon,timeseries1
London,80.5234,121.0452,500
Paris,90.4414,130.0252,400
New York,110.5324,90.0023,700

Now Expected output is 现在预期输出为

File2.csv

Name,lat,lon,timeseries1,timeseries(n) #timeseries is 24 hrs format 17:45:00
London,80.5234,121.0452,500,2103 #Addition of all three values 
Paris,90.4414,130.0252,400,1391
New York,110.5324,90.0023,700,2055

With python, numpy and dictionaries it would be straight as key = sum of values but I want to use Pandas 使用python, numpy and dictionaries ,将直接作为key = sum of values但我想使用Pandas

Please suggest me how to start with or may be a point me to some example. 请建议我如何开始,或者可能是我举一些例子的重点。 I have not see anything like Dictionary types with Pandas with Latitude and Longitude . 我没有看到类似带有LatitudeLongitude Pandas的Dictionary类型。

Perform a groupby aggregation on the first df, call sum and then merge this with the other df: 在第一个df上执行groupby聚合,调用sum ,然后merge其与另一个df merge

In [12]:
gp = df.groupby('Name')['timeseries(n)'].sum().reset_index()
df1.merge(gp, on='Name')

Out[14]:
       Name       Lat       Lon  timeseries1  timeseries(n)
0    London   80.5234  121.0452          500           2103
1     Paris   90.4414  130.0252          400           1391
2  New York  110.5324   90.0023          700           2055

the aggregation looks like this: 聚合如下所示:

In [15]:    
gp

Out[15]:
       Name  timeseries(n)
0    London           2103
1  New York           2055
2     Paris           1391

Your csv files can loaded using read_csv so something like: 您的csv文件可以使用read_csv加载,例如:

df = pd.read_csv('File1.csv')
df1 = pd.read_csv('File2.csv')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM