Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用日期为键的熊猫从csv创建字典_Python_Csv_Dictionary_Pandas - Fatal编程技术网

Python 使用日期为键的熊猫从csv创建字典

Python 使用日期为键的熊猫从csv创建字典,python,csv,dictionary,pandas,Python,Csv,Dictionary,Pandas,我希望从下表创建字典 ID ArCityArCountry DptCityDptCountry DateDpt DateAr 1922 ParisFrance NewYorkUnitedState 2008-03-10 2001-02-02 1002 LosAngelesUnitedState California UnitedState 2008-03-10 2008-12-01 1901 ParisFrance

我希望从下表创建字典

ID    ArCityArCountry         DptCityDptCountry      DateDpt    DateAr
1922  ParisFrance             NewYorkUnitedState     2008-03-10 2001-02-02
1002  LosAngelesUnitedState   California UnitedState 2008-03-10 2008-12-01
1901  ParisFrance             LagosNigeria           2001-03-05 2001-02-02
1922  ParisFrance             NewYorkUnitedState     2011-02-03 2008-12-01
1002  ParisFrance             CaliforniaUnitedState  2003-03-04 2002-03-04
1099  ParisFrance             BeijingChina           2011-02-03 2009-02-04
1901  LosAngelesUnitedState   ParisFrance            2001-03-05 2001-02-02

预期产量
ParisFrance={DateAr,ID,ArCityArCountry,DptCityDptCountry}
注意:我想按ArcityArcCountry和DptCityDptCountry进行分组

你会注意到我没有包括DateDpt;我想选择DateAr和DateDpt之间的所有ID,并且在指定的时间段之间实际位于法国巴黎或美国加利福尼亚州

例如,1999年10月02日,A先生在巴黎呆到2013年12月12日,B先生在巴黎呆到2010年11月04日,然后离开2012年9月09日,这意味着MrA和B先生在巴黎,因为MrB的巴黎之行正好在当时


MrA是否存在
CaliforniaUnitedStates={DateAr,ID,ArcityArcCountry,DptCityDptCountry}

是否要在ParisFrance的字典中列出日期?预期输出ParisFrance={DateAr,ID,ArcityArcCountry,DptCityDptCountry}这将创建一个元组而不是字典。可以根据您的示例输入显示实际结果数据;是的,正如你所说的德米克·穆勒;我想要的只是ArcityArcCountry下每个值的字典,DateAr是索引。如果我遇到这个错误,我可能会给出一个很差的输出:“df.DateDpt=pd.to_datetime(df.DateDpt,格式=“%Y-%m-%d”)文件“C:\Python27\lib\site packages\pandates\core\generic.py”,第2360行,在getattr AttributeError:“DataFrame”对象没有属性“DateDpt”是否要在ParisFrance的字典中列出DateAr?预期的输出ParisFrance={DateAr,ID,ArCityArCountry,DPTCITYDPCountry}这将创建元组而不是字典。可以根据您的示例输入显示实际结果数据;是的,正如你所说的德米克·穆勒;我想要的只是ArcityArcCountry下每个值的字典,DateAr是索引。如果我遇到这个错误,我可能会给出一个很差的输出:“df.DateDpt=pd.to_datetime(df.DateDpt,格式=“%Y-%m-%d”)文件“C:\Python27\lib\site packages\pandates\core\generic.py”,第2360行,在getattr AttributeError:“DataFrame”对象没有属性“DateDpt”
import pandas as pd
import datetime
from pandas_datareader import data, wb
import csv
#import numpy as np

out= open("testfile.csv", "rb")
data = csv.reader(out)
data = [[row[0],row[1] + row[2],row[3] + row[4], row[5],row[6]] for row in data]
out.close()
print data

out=open("data.csv", "wb")
output = csv.writer(out)

for row in data:
    output.writerow(row)

out.close()

df = pd.read_csv('data.csv')
for DateDpt, DateAr in df.iteritems():
    df.DateDpt = pd.to_datetime(df.DateDpt, format='%Y-%m-%d')
    df.DateAr = pd.to_datetime(df.DateAr, format='%Y-%m-%d')
print df
dept_cities = df.groupby('ArCityArCountry')

for city, departures in dept_cities:
    print(city)
    print([list(r) for r in departures.loc[:, ['AuthorID', 'DptCityDptCountry', 'DateDpt', 'DateAr']].to_records()])