gpt4 book ai didi

pandas series.str.sum 自动忽略非法数据

转载 作者:行者123 更新时间:2023-12-01 13:10:18 25 4
gpt4 key购买 nike

数据

data = [
{"content": "1", "title": "appstore", "info": "", "time": 1578877014},
{"content": "2", "title": "app", "info": "", "time": 1579877014},
{"content": "3", "title": "a", "info": "", "time": 1582877014},
{"content": "12", "title": "jack", "info": "", "time": 1582876014},
{"content": "aa", "title": "apple", "info": "", "time": 1581877014},
{"content": "16", "title": "banana", "info": "", "time": 1561877014},
]

我的代码

s = pd.Series(data)

print('-'*100)
print(s.str.get('content').sum())

我也觉得这很疯狂,但是数据确实有脏数据。我希望数据

1+2+3+12+16=36
## calc sum automatically ignore illegal data
{"content": "aa", "title": "apple", "info": "", "time": 1581877014}

最佳答案

使用to_numeric使用 errors='coerce' 将错误值替换为 NaN 并将字符串数字转换为数字,因此输出为:

print(pd.to_numeric(s.str.get('content'), errors='coerce').sum())
34.0

关于pandas series.str.sum 自动忽略非法数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60503918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com