dataframe values.tolist()数据类型 [英] dataframe values.tolist() datatype
问题描述
我有一个像这样的数据框:
I have a dataframe like this:
此数据框有几列. float
类型有两个:price
和change
,而volme
和amount
是int
类型.
我使用方法df.values.tolist()
更改df列出并获取数据:
This dataframe has several columns. Two are of type float
: price
and change
, while volme
and amount
are 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屋!