我想你可以從提取key
name
首先每個dict
,然後轉換成string
to_datetime
。最後使用boolean indexing
與between
:
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
我怎樣才能找到出什麼感覺nt數據屬於這個子集? – user2105555
如果'df1 = df.query('locations ==「波士頓都會區域」和「2016-09-01」
jezrael
我不確定,但似乎錯字'df.to_datetime' - 需要'pd.to_datetime'。 – jezrael