给出一个数据框,例如:
page source_url count yes no ii dk
google.com youtube.com 3 0 0 1 2
facebook.com whatsapp.com 3 0 0 1 2
microsoft.com stackoverflow.com 3 0 0 1 2
我只想在最后4列上应用pd.to_numeric
。
我尝试做:
df['count'] = pd.to_numeric(df['count'])
df['yes'] = pd.to_numeric(df['yes'])
df['no'] = pd.to_numeric(df['no'])
df['ii'] = pd.to_numeric(df['i'])
df['dk'] = pd.to_numeric(df['dk'])
但是第一行一直在给我:
ValueError at /
Unable to parse string "<bound method Series.count of page https://www.snopes.com/fact-check/trump-suicid...
source_url https://www.facebook.com/candace.witte/posts/1...
count 0
Name: 7909, dtype: object>" at position 0
如何在数据框的某些列上使用to_numeric()
?
答案 0 :(得分:1)
您的列count
似乎至少具有一个非数字值,在这种情况下,该值以“绑定方法Series.count ...”开头。要将错误变成nan
,您可以执行以下操作:
df['count'] = df.count.apply(pd.to_numeric, errors='coerce')
如果更多列存在相同的问题,则可以对它们执行相同的操作