开发者社区> 问答> 正文

Pandas 系列获取所有星期一数据?

我的资料

data= [{"content": "11", "title": "刘德华", "info": "2020-01-13", "time": 1578877014},
                  {"content": "22", "title": "刘德", "info": "2020-01-24", "time": 1579877014},
                  {"content": "33", "title": "apple", "info": "2020-02-28", "time": 1582877014},
                  {"content": "55", "title": "app", "info": "2020-02-17", "time": 1581877014},
                  {"content": "66", "title": "appstore", "info": "2019-06-30", "time": 1561877014},
                  {"content": "44", "title": "banana", "info": "2020-02-28", "time": 1582876014},
                  {"content": "aa", "title": "banana", "info": "2020-03-12 eee", "time": 1584000882},
                  {"content": "bb", "title": "Thursday data", "info": "2018-03-12 vvvv", "time": 1520842482},
                  {"content": "cc", "title": "banana", "info": "2020-03-14 xxx", "time": 1584154305},
                  {"content": "cc", "title": "banana", "info": "2019-03-14 aa", "time": 1552531905},
                  {"content": "cc", "title": "Thursday data", "info": "2020-03-19 data", "time": 1584586305},
                  {"content": "cc", "title": "Thursday data", "info": "2019-11-07 aaa", "time": 1573095105},

                  ]

我想获取所有星期四的数据,不仅包含今年,我希望数据是

                  {"content": "bb", "title": "Thursday data", "info": "2018-03-12 bbb", "time": 1520842482},
                  {"content": "cc", "title": "Thursday data", "info": "2020-03-19 aaa ", "time": 1584586305},
                  {"content": "cc", "title": "Thursday data", "info": "2019-11-07 
 aaa", "time": 1573095105},

无法提供想法,谢谢

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 16:46:39 531 0
1 条回答
写回答
取消 提交回答
  • Series.str.get与带有日期时间的Series.eqSeries.dt.day_name进行比较:

    s = pd.Series(L)
    
    s1 = s[pd.to_datetime(s.str.get('time'), unit='s').dt.day_name() == 'Thursday']
    print (s1)
    6     {'content': 'aa', 'title': 'banana', 'info': '...
    9     {'content': 'cc', 'title': 'banana', 'info': '...
    10    {'content': 'cc', 'title': 'Thursday data', 'i...
    11    {'content': 'cc', 'title': 'Thursday data', 'i...
    dtype: object
    

    细节

    print (s.str.get('time'))
    0     1578877014
    1     1579877014
    2     1582877014
    3     1581877014
    4     1561877014
    5     1582876014
    6     1584000882
    7     1520842482
    8     1584154305
    9     1552531905
    10    1584586305
    11    1573095105
    dtype: int64
    
    print (pd.to_datetime(s.str.get('time'), unit='s'))
    0    2020-01-13 00:56:54
    1    2020-01-24 14:43:34
    2    2020-02-28 08:03:34
    3    2020-02-16 18:16:54
    4    2019-06-30 06:43:34
    5    2020-02-28 07:46:54
    6    2020-03-12 08:14:42
    7    2018-03-12 08:14:42
    8    2020-03-14 02:51:45
    9    2019-03-14 02:51:45
    10   2020-03-19 02:51:45
    11   2019-11-07 02:51:45
    dtype: datetime64[ns]
    

    *

    print (pd.to_datetime(s.str.get('time'), unit='s').dt.day_name()) 0 Monday 1 Friday 2 Friday 3 Sunday 4 Sunday 5 Friday 6 Thursday 7 Monday 8 Saturday 9 Thursday 10 Thursday 11 Thursday dtype: object

    回答来源:stackoverflow

    2020-03-23 16:46:44
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
中文:即学即用的Pandas入门与时间序列分析 立即下载
即学即用的Pandas入门与时间序列分析 立即下载
低代码开发师(初级)实战教程 立即下载