熊猫-自定义类型

时间:2020-05-15 09:44:48

标签: python python-3.x pandas dataframe

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

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

非常感谢!

1 个答案:

答案 0 :(得分:1)

您应该使用to_datetimeas_type函数。 请注意,col2的声明方式首先是object系列。然后,您需要先转换为日期时间,然后再转换为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
相关问题