简体   繁体   English

在 python 列中将这些对象转换为 int64

[英]Convert these Objects to int64 in python columns

Another simple question.另一个简单的问题。 I have to clean up some data, and a few of the columns need to be in int64 format instead of the objects that they are now (example provided).我必须清理一些数据,其中一些列需要采用 int64 格式,而不是它们现在的对象(提供了示例)。 how would I go about uniformly re-formatting these columns.我将如何统一重新格式化这些列。

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

You could parse with regex:你可以用正则表达式解析:

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'])

The function above takes all the unique values from the series and pairs them with its float equivalent.上面的函数从系列中获取所有唯一值,并将它们与其等效的浮点数配对。 This is an extremely efficient approach in the case of repeated values.在重复值的情况下,这是一种非常有效的方法。 The function then maps these value pairs ( map_dict ) to the original series ( s ).然后,该函数将这些值对 ( map_dict ) 映射到原始系列 ( s )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM