python - pandas series.gt 如何使用
问题描述
数据:
a= [{"content": 1, "time": 1577870427}, {"content": 4, "time": 1577870427},
{"content": 2, "time": 1577956827},
{"content": 3, "time": 1580548827}, {"content": 5, "time": 1580635227},
{"content": 6, "time": 1583054427}, {"content": 7, "time": 1583140827}]
我希望内容超过 5
最终数据
[{"content": 6, "time": 1583054427}, {"content": 7, "time": 1583140827}]
我的代码
index = pd.to_datetime([i['time'] for i in a], unit='s')
df = pd.Series(a,index)
df.gt(5)
但引发错误
解决方案
问题在于您的系列中的数据是字典中的数据,因此在 pandas 中确实不容易处理,也可能仅在循环中(apply
或list comprehension
或for
)。
index = pd.to_datetime([i['time'] for i in a], unit='s')
df = pd.Series(a,index)
print (df.head().apply(type))
2020-01-01 09:20:27 <class 'dict'>
2020-01-01 09:20:27 <class 'dict'>
2020-01-02 09:20:27 <class 'dict'>
2020-02-01 09:20:27 <class 'dict'>
2020-02-02 09:20:27 <class 'dict'>
dtype: object
如果想要过滤,可以通过提取content
到Series
标量然后进行比较:
print (df[df.str.get('content').gt(5)])
2020-03-01 09:20:27 {'content': 6, 'time': 1583054427}
2020-03-02 09:20:27 {'content': 7, 'time': 1583140827}
dtype: object
它是如何工作的:
print (df.str.get('content'))
2020-01-01 09:20:27 1
2020-01-01 09:20:27 4
2020-01-02 09:20:27 2
2020-02-01 09:20:27 3
2020-02-02 09:20:27 5
2020-03-01 09:20:27 6
2020-03-02 09:20:27 7
dtype: int64
print (df.str.get('content').gt(5))
2020-01-01 09:20:27 False
2020-01-01 09:20:27 False
2020-01-02 09:20:27 False
2020-02-01 09:20:27 False
2020-02-02 09:20:27 False
2020-03-01 09:20:27 True
2020-03-02 09:20:27 True
dtype: bool
如果要处理数据需要apply
自定义功能:
def f(x):
x['time'] = pd.to_datetime(x['time'], unit='s')
return x
df = df.apply(f)
print (df)
2020-01-01 09:20:27 {'content': 1, 'time': 2020-01-01 09:20:27}
2020-01-01 09:20:27 {'content': 4, 'time': 2020-01-01 09:20:27}
2020-01-02 09:20:27 {'content': 2, 'time': 2020-01-02 09:20:27}
2020-02-01 09:20:27 {'content': 3, 'time': 2020-02-01 09:20:27}
2020-02-02 09:20:27 {'content': 5, 'time': 2020-02-02 09:20:27}
2020-03-01 09:20:27 {'content': 6, 'time': 2020-03-01 09:20:27}
2020-03-02 09:20:27 {'content': 7, 'time': 2020-03-02 09:20:27}
dtype: object
所以更好的是创建DataFrame
:
df = pd.DataFrame(a)
print (df)
content time
0 1 1577870427
1 4 1577870427
2 2 1577956827
3 3 1580548827
4 5 1580635227
5 6 1583054427
6 7 1583140827
然后很容易处理,例如比较,因为标量:
print (df['content'].gt(5))
0 False
1 False
2 False
3 False
4 False
5 True
6 True
Name: content, dtype: bool
df['time'] = pd.to_datetime(df['time'], unit='s')
print (df)
content time
0 1 2020-01-01 09:20:27
1 4 2020-01-01 09:20:27
2 2 2020-01-02 09:20:27
3 3 2020-02-01 09:20:27
4 5 2020-02-02 09:20:27
5 6 2020-03-01 09:20:27
6 7 2020-03-02 09:20:27
推荐阅读
- mysql - 如何在一周中的所有日子里将表格置于第三范式
- database - 在 SNOWFLAKE 中为 QA 和 dev 区域创建单独的 Web 链接
- flutter - TweenAnimationBuilder 动画太快
- azure-active-directory - 使用应用程序 ID 从 Java 应用程序调用 Microsoft Graph 时出现禁止错误
- npm - Npm 新手。为什么 npm 命令不起作用?
- azure - Azure SQL Server 异地复制资源
- javascript - 我收到带有 innerHTML 的跨站点脚本 (XSS) 警告
- selenium - 当我终止任务调度程序任务时,有没有办法调用内部函数?
- bamboo - 竹文件路径作为字符串传递
- sql-server - 在变量中使用引号在 SQL 中构造字符串