Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫-自定义类型_Python_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 熊猫-自定义类型

Python 熊猫-自定义类型,python,python-3.x,pandas,dataframe,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_dat

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

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
非常感谢

应该使用to_datetime和as_类型函数。 注意,按照声明的方式,col2首先是一个对象序列。然后需要先转换为datetime,然后再转换为int。从object到int的直接转换不起作用

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

抱歉,但我不清楚到底是什么问题?你能解释一下吗?类似于输出v/s预期输出?@Anshul编辑以显示示例数据。我不明白,您的答案。对于第一列,我想将历元转换为日期时间str。对于第二列,我想将str转换为int。代码就是这样做的:第一部分用声明为对象类型的两列定义数据帧。函数printdf.dtypes显示每列的数据类型。第二部分使用函数to_datetime将col1转换为datetime,然后使用to_datetime和as_type'int64'函数将col2转换为int。我们最后展示了数据帧的值和类型。您可以看到col1的类型是datetime64[ns],col2的类型是int64在查看了to_datetime之后,我现在明白了您所做的事情。谢谢