Python DataFrame对象类型列为int或float错误
我有以下数据帧Python DataFrame对象类型列为int或float错误,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下数据帧 <class 'pandas.core.frame.DataFrame'> RangeIndex: 20 entries, 0 to 19 Data columns (total 7 columns): Borough 20 non-null object Indian 20 non-null object Pakistani 20 non-null object Bangladeshi 20 non-null objec
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20 entries, 0 to 19
Data columns (total 7 columns):
Borough 20 non-null object
Indian 20 non-null object
Pakistani 20 non-null object
Bangladeshi 20 non-null object
Chinese 20 non-null object
Other_Asian 20 non-null object
Total_Asian 20 non-null object
dtypes: object(7)
错误是:
基数为10的int()的文本无效:
我也试过了
df_LondonEthnicity['Indian'] = df_LondonEthnicity.astype({'Indian': int}).dtypes
我也试过了
cols = ['Indian', 'Pakistani', 'Bangladeshi', 'Chinese', 'Other_Asian', 'Total_Asian']
for col in cols: # Iterate over chosen columns
df_LondonEthnicity[col] = pd.to_numeric(df_LondonEthnicity[col])
还尝试将get字符串转换为float
我希望能在这方面得到一些帮助。谢谢正如评论中所指出的,您需要使用
to_numeric
功能
错误的意思是,您试图转换的值包含的字符不是0-9
(base10)
因此,您可以选择使用pd.to\u numeric
,并将所有不一致值设置为NaN
,或者以某种方式将其转换
假设您有这样一个数据帧
>>df
X
0 123
1 123,
2 200
3 200.1
使用
pd.to\u numeric
将生成这样的输出。但这些值是浮动的
>pd.到_numeric(df.X,errors='concurve')
0 123.0
1楠
2 200.0
3 200.1
名称:X,数据类型:float64
另一个选择是以这样的方式转换它 df.X.str.extract(r'([\d]+)).astype(int) 0 0 123 1 123 2 200 3 200
正如评论中所指出的,您需要使用
to_numeric
功能
错误的意思是,您试图转换的值包含的字符不是0-9
(base10)
因此,您可以选择使用pd.to\u numeric
,并将所有不一致值设置为NaN
,或者以某种方式将其转换
假设您有这样一个数据帧
>>df
X
0 123
1 123,
2 200
3 200.1
使用
pd.to\u numeric
将生成这样的输出。但这些值是浮动的
>pd.到_numeric(df.X,errors='concurve')
0 123.0
1楠
2 200.0
3 200.1
名称:X,数据类型:float64
另一个选择是以这样的方式转换它 df.X.str.extract(r'([\d]+)).astype(int) 0 0 123 1 123 2 200 3 200
您能否提供
df_Londonocidentity
的样本数据<代码>pd。转换为数值是正确的转换工具。如果pandas
没有自动将列解析为数字数据类型,则astype
将不会有帮助to_numeric
允许您将所有非数字数据转换为NaN
您在df_londonocidentity['Indian']
中有什么?如果存在空字符串或带有文本而不是数字的字符串,则您无法转换它。您可以提供df_londonocidentity
的示例数据吗<代码>pd。转换为数值是正确的转换工具。如果pandas
没有自动将列解析为数字数据类型,则astype
将不会有帮助to_numeric
允许您将所有非数字数据转换为NaN
您在df_londonocidentity['Indian']
中有什么?如果存在空字符串或带有文本而不是数字的字符串,则无法转换它。除“Borough”之外的所有列都包含数字,因此应该是int或float,以便我可以进行一些数学计算。我正在抓取网页上的信息。url=''连接到url response=requests.get(url)#解析HTML并保存到BeautifulSoup对象soup=BeautifulSoup(response.text,“HTML.parser”)#这是一个漂亮的soup对象,它解析了整个HTML文本table=soup.find_all('table')[4]我尝试了你的方法,它将我所有的数据转换成了NaN。明白了。我的号码是12456格式的。我已经删除了逗号,现在它起作用了。谢谢你的帮助。除“Borough”之外的所有列都包含数字,因此应该是int或float,以便我可以进行一些数学计算。我正在抓取网页上的信息。url=''连接到url response=requests.get(url)#解析HTML并保存到BeautifulSoup对象soup=BeautifulSoup(response.text,“HTML.parser”)#这是一个漂亮的soup对象,它解析了整个HTML文本table=soup.find_all('table')[4]我尝试了你的方法,它将我所有的数据转换成了NaN。明白了。我的号码是12456格式的。我已经删除了逗号,现在它起作用了。谢谢你的帮助。
cols = ['Indian', 'Pakistani', 'Bangladeshi', 'Chinese', 'Other_Asian', 'Total_Asian']
for col in cols: # Iterate over chosen columns
df_LondonEthnicity[col] = pd.to_numeric(df_LondonEthnicity[col])