Reputation: 83
I have the following code
df = pd.DataFrame()
cols = ['Column1', 'Column2', 'Column3', 'Column4']
df = df[cols]
And I'm getting the following error to the last line:
KeyError: "['Column1' 'Column2' 'Column3'\n 'Column4'] not in index
I don't know why it's adding that newline after Column3 or if that is even the issue. Thanks!
Upvotes: 1
Views: 3549
Reputation: 1442
In order to initialize an empty data frame with named columns, you pass the columns as an argument:
cols = ['Column1', 'Column2', 'Column3', 'Column4']
df = pd.DataFrame(columns=cols)
Upvotes: 2