Pandas自定义类型

2024-04-20 00:03:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有一个数据框,我希望将列转换为特定的数据格式。我有一个方法可以将数据转换为各种类型。我的代码目前不完整,因为我不确定如何迭代抛出列行并相应地转换数据

def _get_mappings(mapping_dict):

    json_data = pd.json_normalize(api_response)
    tmp_dataframe = pd.DataFrame()

    for mapping_item in mapping_dict:
        json_data[mapping_item["field"]] = _parse_data_types(json_data["created_time"], mapping_item["type"])

        # Do some other stuff...


def _parse_data_types(pandas_column, field_type):

    # How do I iterate the rows for each column and covert them to the different types
    # as shown below? I may have more return types in the future.

    if field_type == "str":
        field_data = str(field_data)

    elif field_type == "int":
        field_data = int(field_data)

    # Converts 13-digit epoch to a datetime string. It is a str.
    elif field_type == "datetime":
        field_data = epoch_to_datestr(field_data)

    return pandas_column

编辑的样本数据:

# Just using list as an example as I am unsure how pandas stores it columns.

input date column: [1589537024000, 1589537025000, 1589537026000]  # epoch as integer
output date column: ["2020-05-15 10:03:44", "2020-05-15 10:03:45", "2020-05-15 10:03:46"]  # string

input kg column: ["123", "456", "789"]  # string
output kg column: [123, 456, 789]  # integers

非常感谢


Tags: theto数据jsonfieldpandasdataas
1条回答
网友
1楼 · 发布于 2024-04-20 00:03:27

您应该使用to_datetimeas_type函数。 注意,它的声明方式,col2首先是一个object序列。然后需要先转换为datetime,然后再转换为int。从objectint的直接转换不起作用

df = pd.DataFrame([[1589537024000, "2020-05-15 10:03:44"],
                   [1589537025000, "2020-05-15 10:03:45"],
                   [1589537026000, "2020-05-15 10:03:46"]],
                  columns=['col1', 'col2'], dtype=object)
print(df)
print(df.dtypes)
df['col1'] = pd.to_datetime(df['col1'], unit='ms')
df['col2'] = pd.to_datetime(df['col2']).values.astype('int64') // 10 ** 6
print(df)
print(df.dtypes)

输出:

            col1                 col2
0  1589537024000  2020-05-15 10:03:44
1  1589537025000  2020-05-15 10:03:45
2  1589537026000  2020-05-15 10:03:46
col1    object
col2    object
dtype: object
                 col1           col2
0 2020-05-15 10:03:44  1589537024000
1 2020-05-15 10:03:45  1589537025000
2 2020-05-15 10:03:46  1589537026000
col1    datetime64[ns]
col2             int64
dtype: object

相关问题 更多 >