Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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 如何将数据帧中的所有对象类型值转换为int_Python_Pandas_Dataframe_Int - Fatal编程技术网

Python 如何将数据帧中的所有对象类型值转换为int

Python 如何将数据帧中的所有对象类型值转换为int,python,pandas,dataframe,int,Python,Pandas,Dataframe,Int,有人知道如何将dataframe中的所有列值转换为int吗 假设我有一个包含 A B C 1 2 1 3 2 1 1 4 5 数据类型是对象如何将其转换为int或numeric您可以使用pandas astype()函数,pandas to_numeric也可以提供您想要的行为 a = pd.DataFrame({"A":["1",2,3], "B":[2,"2",4], "C":[1.0,1.0,5.0]}) a.dtypes Out[8]: A obje

有人知道如何将dataframe中的所有列值转换为int吗

假设我有一个包含

  A B C 

  1 2 1

  3 2 1

  1 4 5
数据类型是对象如何将其转换为int或numeric

您可以使用pandas astype()函数,pandas to_numeric也可以提供您想要的行为

a = pd.DataFrame({"A":["1",2,3], "B":[2,"2",4], "C":[1.0,1.0,5.0]})
a.dtypes

Out[8]: 
A     object
B     object
C    float64
dtype: object

b = a.astype(int)
b.dtypes
Out[10]: 
A    int32
B    int32
C    int32
dtype: object

b
Out[11]: 
   A  B  C
0  1  2  1
1  2  2  1
2  3  4  5
您可以使用pandas astype()函数,pandas to_numeric也可以为您提供所需的行为

a = pd.DataFrame({"A":["1",2,3], "B":[2,"2",4], "C":[1.0,1.0,5.0]})
a.dtypes

Out[8]: 
A     object
B     object
C    float64
dtype: object

b = a.astype(int)
b.dtypes
Out[10]: 
A    int32
B    int32
C    int32
dtype: object

b
Out[11]: 
   A  B  C
0  1  2  1
1  2  2  1
2  3  4  5

您可以使用pandas模块pd.to_numeric(df)将对象转换为数值。
df=df.astype(int)
您可以使用pandas模块pd.to_numeric(df)将对象转换为数值。
df=df.astype(int)