dataframe values.tolist()数据类型 [英] dataframe values.tolist() datatype

查看:5811
本文介绍了dataframe values.tolist()数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据框:

I have a dataframe like this:

此数据框有几列. float类型有两个:pricechange,而volmeamountint类型. 我使用方法df.values.tolist()更改df列出并获取数据:

This dataframe has several columns. Two are of type float: price and change, while volme and amountare of type int. I use the method df.values.tolist() change df to list and get the data:

datatmp = df.values.tolist()
print(datatmp[0])

[20160108150023.0, 11.12, -0.01, 4268.0, 4746460.0, 2.0]

df中的int类型全部更改为float类型. 我的问题是为什么int类型更改为float类型?如何获取所需的int数据?

The int types in df all change to float types. My question is why do int types change to the float types? How can I get the int data I want?

推荐答案

您可以按列转换:

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()

很有可能您的列amount的类型为float.您在此列中是否有任何NaN?这些始终为float类型,并将使整个列为float.

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.

您可以使用以下方法将其投射到int:

You can cast to int with:

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

这篇关于dataframe values.tolist()数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆