0

So I have 2 datasets: 1. an array of data (call it x), 2. a pandas dataframe with n columns

I am trying to write a for loop, which goes through each column in my dataframe and calculates the correlation coefficient from that column against my first array of data, x.

x = [1.2, 1.9, 2.2, 4.1]
y = {'col1': [1, 2, 3, 4], 'col2': [10, 20, 30, 40]}
example_df = pd.DataFrame(data = y)
for i in example_df.columns:
 print(np.corrcoef(x, example_df.i)[0, 1])

However, I am receiving an error

"'DataFrame' object has no attribute 'i'"

I am wondering if anyone has a suggestion for how to access and use 'i' to access columns in my dataframe.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47

1 Answers1

1

When you write this:

example_df.i

… that's asking for a column literally named i. And of course you don't have one.

What you have a column named col1, and i is a variable holding the string 'col1'. To ask for that, do this:

example_df[i]
abarnert
  • 354,177
  • 51
  • 601
  • 671