[英]Convert these Objects to int64 in python columns
另一個簡單的問題。 我必須清理一些數據,其中一些列需要采用 int64 格式,而不是它們現在的對象(提供了示例)。 我將如何統一重新格式化這些列。
print(data.Result)
0 98.8 PG/ML H
1 8.20000
2 26.8 PG/ML H
3 40.8 PG/ML H
4 CREDIT
5 15.30000
你可以用正則表達式解析:
import re
def parse_int(s):
"""
A fast memoized function which builds a lookup dictionary then maps values to the series
"""
map_dict = {x:float(re.findall('[0-9.]+',x)[0]) for x in s.unique() if re.search('[0-9.]+',x)}
return s.map(map_dict)
data['Result'] = parse_int(data['Result'])
上面的函數從系列中獲取所有唯一值,並將它們與其等效的浮點數配對。 在重復值的情況下,這是一種非常有效的方法。 然后,該函數將這些值對 ( map_dict
) 映射到原始系列 ( s
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.