dataframe values.tolist() datatype

前端 未结 2 774
栀梦
栀梦 2021-01-01 07:27

I have a dataframe like this:

This dataframe has several columns. Two are of type float: price and change, while

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-01 07:45

    You can convert column-by-column:

    by_column = [df[x].values.tolist() for x in df.columns]
    

    This will preserve the data type of each column.

    Than convert to the structure you want:

    list(list(x) for x in zip(*by_column))
    

    You can do it in one line:

    list(list(x) for x in zip(*(df[x].values.tolist() for x in df.columns)))
    

    You can check what datatypes your columns have with:

    df.info()
    

    Very likely your column amount is of type float. Do you have any NaN in this column? These are always of type float and would make the whole column float.

    You can cast to int with:

    df.values.astype(int).tolist()
    

提交回复
热议问题