1
$\begingroup$

I am a very inexperienced programmer, this is my first question on the Data Science StackExchange, I sorry if it is formatted poorly or comes across as basic. For some strange reason, in Python, whenever I try to run a correlation function on the population density & total cases per million columns of my COVID-19 DataFrame (which I imported/read into Spyder as a csv), I keep getting the same long error message, namely, "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." regardless of whether I use the correlation function from Pandas or Numpy. My first thought was that this error was caused by the presence of null values in those columns, so I used df.dropna(), then ran the correlation function again but I got the same "ValueError", so I have no idea what is going on here, I was able to run a correlation on those same columns just fine in RStudio which I am equally unskilled and inexperienced with.

enter image description here

$\endgroup$
  • $\begingroup$ Try to add method ='pearson' in your parenthesis, this way : df.corr(df[...], df[...], method ='pearson'). Not sure if it will work but there seems to be an error on which method you use / on the fact that you don't precise the method for the corr function $\endgroup$ – BeamsAdept Oct 6 '20 at 9:14
  • $\begingroup$ @BeamsAdept thank you for the suggestion, I went ahead and tried running it with the extra pearson argument and it did get rid of the ValueError which is great! Unfortunately, now it is saying there is a TypeError, this is the exact output now: In [9]: df.corr(df['population_density'], df['total_cases_per_million'], ...: method = 'pearson') Traceback (most recent call last): File "<ipython-input-9-236734901706>", line 2, in <module> method = 'pearson') TypeError: corr() got multiple values for argument 'method' $\endgroup$ – Marlen Oct 6 '20 at 9:42
1
$\begingroup$

You should look at the documentation. You do not pass the column names as an argument.

subset_df = df[['col1', 'col2']]
subset_df.corr()

That should solve this for you.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.