0

Given a dataframe such as:

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

I want to apply pd.to_numeric on the last 4 columns only.

I tried doing :

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'])

But the first line keeps giving me:

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

How can I use to_numeric() on some columns of a dataframe?

2
  • Did you perhaps use df.count in your code, either currently or in a prior function or iteration, to assign to the count column? It looks like you may have overwritten something in your namespace with the dataframe.count function signature, based on string <bound method Series.count... Commented Jul 19, 2019 at 16:29
  • no I didnt use .count anywhere Commented Jul 19, 2019 at 16:32

1 Answer 1

1

It seems your column count has at least one non-numeric value, in this case one that starts with "bound method Series.count...". To turn errors into nan you could do the following:

df['count'] =  df.count.apply(pd.to_numeric, errors='coerce')

If more columns have the same problem, you could do the same for them

Sign up to request clarification or add additional context in comments.

1 Comment

Thx man. I checked the csv and there was an issue with the script that generated it. I fixed it and now the code works

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.