2017-01-30 63 views
1

我試圖使用兩列標準來對數據集進行子集化,但是我的結果會產生錯誤。任何想法(S)爲什麼?這是我的代碼:Python - 使用兩列標準子集數據集

df[(df['locations'] = 'New York City Metro Area') & (2016-09-01 < df['publication_date'] < 2016-09-30)] 

這是我的錯誤:

f = lambda x, y: lib.ismember(x, set(values)) 
    File "pandas\lib.pyx", line 158, in pandas.lib.ismember (pandas\lib.c:5199) 
TypeError: unhashable type: 'list' 

如果有幫助,我的數據看起來是這樣的:

df['publication_date'].head() 

0 2017-01-30T04:48:11.929095Z 
1   2016-11-15T05:30:03Z 
2 2017-01-30T04:45:24.861067Z 
3 2017-01-30T04:47:41.419255Z 
4 2017-01-30T04:49:36.192148Z 
Name: publication_date, dtype: object 

df['locations'].head() 

0  [{'name': 'Kansas City, MO'}] 
1   [{'name': 'Evanston, IL'}] 
2   [{'name': 'Stamford, CT'}] 
3    [{'name': 'Reno, NV'}] 
4 [{'name': 'Boston Metro Area'}] 
Name: locations, dtype: object 

回答

2

我想你可以從提取keyname首先每個dict,然後轉換成stringto_datetime。最後使用boolean indexingbetween

df = pd.DataFrame({'locations':[[{'name': 'Kansas City, MO'}], [{'name': 'Evanston, IL'}], [{'name': 'Stamford, CT'}],[{'name': 'Reno, NV'}],[{'name': 'Boston Metro Area'}]], 
        'publication_date':['2017-01-30T04:48:11.929095Z','2016-11-15T05:30:03Z','2017-01-30T04:45:24.861067Z','2017-01-30T04:47:41.419255Z','2017-01-30T04:49:36.192148Z']}) 
print (df) 
         locations    publication_date 
0 [{'name': 'Kansas City, MO'}] 2017-01-30T04:48:11.929095Z 
1  [{'name': 'Evanston, IL'}]   2016-11-15T05:30:03Z 
2  [{'name': 'Stamford, CT'}] 2017-01-30T04:45:24.861067Z 
3   [{'name': 'Reno, NV'}] 2017-01-30T04:47:41.419255Z 
4 [{'name': 'Boston Metro Area'}] 2017-01-30T04:49:36.192148Z 

print (type(df.locations.iloc[0])) 
<class 'list'> 


df.locations = df.locations.apply(lambda x: x[0]['name']) 
df.publication_date = pd.to_datetime(df.publication_date) 
print (df) 
      locations   publication_date 
0 Kansas City, MO 2017-01-30 04:48:11.929095 
1  Evanston, IL 2016-11-15 05:30:03.000000 
2  Stamford, CT 2017-01-30 04:45:24.861067 
3   Reno, NV 2017-01-30 04:47:41.419255 
4 Boston Metro Area 2017-01-30 04:49:36.192148 

print (df[(df['locations'] == 'Boston Metro Area') & 
      (df['publication_date'].between('2016-09-01', '2018-09-30'))]) 
      locations   publication_date 
4 Boston Metro Area 2017-01-30 04:49:36.192148 

解決方案與query

print (df.query('locations == "Boston Metro Area" and "2016-09-01" < publication_date < "2018-09-30"')) 
      locations   publication_date 
4 Boston Metro Area 2017-01-30 04:49:36.192148 

如果不需要更改值的結構locations列:

df.publication_date = pd.to_datetime(df.publication_date) 
print (df) 
         locations   publication_date 
0 [{'name': 'Kansas City, MO'}] 2017-01-30 04:48:11.929095 
1  [{'name': 'Evanston, IL'}] 2016-11-15 05:30:03.000000 
2  [{'name': 'Stamford, CT'}] 2017-01-30 04:45:24.861067 
3   [{'name': 'Reno, NV'}] 2017-01-30 04:47:41.419255 
4 [{'name': 'Boston Metro Area'}] 2017-01-30 04:49:36.192148 

print (df[(df['locations'].apply(lambda x: x[0]['name']) == 'Boston Metro Area') & 
      (df['publication_date'].between('2016-09-01', '2018-09-30'))]) 

         locations   publication_date 
4 [{'name': 'Boston Metro Area'}] 2017-01-30 04:49:36.192148 
+1

我怎樣才能找到出什麼感覺nt數據屬於這個子集? – user2105555

+1

如果'df1 = df.query('locations ==「波士頓都會區域」和「2016-09-01」 jezrael

+1

我不確定,但似乎錯字'df.to_datetime' - 需要'pd.to_datetime'。 – jezrael