[
{"content": "33", "title": "banana", "info": "2020-03-12", "time": 1584000882},
{"content": "44", "title": "banana", "info": "2018-03-12", "time": 1520842482},
{"content": "55", "title": "banana", "info": "2020-03-14", "time": 1584154305},
{"content": "66", "title": "banana", "info": "2019-03-14", "time": 1552531905},
{"content": "77", "title": "banana", "info": "2020-03-19", "time": 1584586305},
{"content": "77", "title": "banana", "info": "2012-03-05", "time": 1330934123},
]
3 Réponses :
Filtrer par mois par DatetimeIndex.month :
s[s.index.month_name == 'March']
Ou par DatetimeIndex.month_name :
s[s.index.month == 3]
En gros, vous pouvez procéder comme suit.
content info time title 6 33 2020-03-12 1584000882 banana 7 44 2018-03-12 1520842482 banana 8 55 2020-03-14 1584154305 banana 9 66 2019-03-14 1552531905 banana 10 77 2020-03-19 1584586305 banana 12 77 2012-03-05 1330934123 banana
Résultats:
df[df['info'].str.contains('-03-')]
Pour le filtrage par mois, si ce n'est qu'une seule fois et que vous pas besoin d'analyser la chaîne de date, vous pouvez simplement utiliser le format de chaîne de date pour économiser l'effort:
content info time title 5 44 2020-02-28 1582876014 banana 6 33 2020-03-12 1584000882 banana 7 44 2018-03-12 1520842482 banana 8 55 2020-03-14 1584154305 banana 9 66 2019-03-14 1552531905 banana 10 77 2020-03-19 1584586305 banana 11 88 2019-11-07 1573095105 banana 12 77 2012-03-05 1330934123 banana
Résultats:
import pandas df = pandas.DataFrame(data) df[df['title']=='banana']
convertir en dataframe et exécuter
import pandas as pd
df = pd.DataFrame.from_dict(data)
df['info'] = pd.to_datetime(df['info'])
#enter specific to_datetime parameters if needed
#filter on month = 3
updated_df = df[df['info'].apply(lambda x:x.month==3)]
print(updated_df.to_dict('records'))
output
[
{'content': '33', 'info': Timestamp('2020-03-12 00:00:00'), 'time': 1584000882, 'title': 'banana'},
{'content': '44', 'info': Timestamp('2018-03-12 00:00:00'), 'time': 1520842482, 'title': 'banana'},
{'content': '55', 'info': Timestamp('2020-03-14 00:00:00'), 'time': 1584154305, 'title': 'banana'},
{'content': '66', 'info': Timestamp('2019-03-14 00:00:00'), 'time': 1552531905, 'title': 'banana'},
{'content': '77', 'info': Timestamp('2020-03-19 00:00:00'), 'time': 1584586305, 'title': 'banana'},
{'content': '77', 'info': Timestamp('2012-03-05 00:00:00'), 'time': 1330934123, 'title': 'banana'}
]