Reputation: 534
I have the following code:
import pandas as pd
df = pd.read_csv("14_5.csv")
print(df.head())
Price,Date,Ticker
104.0,2016-07-01,A
104.87815067615534,2016-07-05,A
104.41190933506331,2016-07-06,A
104.93195657145004,2016-07-07,A
104.42127356374375,2016-07-08,A
When I add:
prices = df.Price
to the code, I get:
AttributeError: 'DataFrame' object has no attribute 'Price'
What am I doing wrong? I want to save each column as a variable.
Upvotes: 0
Views: 846
Reputation: 879661
skipinitialspace=True
tells pd.read_csv
to skip spaces after delimiter.
Notice the difference it makes to the column labels:
In [165]: pd.read_csv('14_5.csv').columns.tolist()
Out[165]: [' Price', 'Date', 'Ticker']
In [167]: pd.read_csv('14_5.csv', skipinitialspace=True).columns.tolist()
Out[167]: ['Price', 'Date', 'Ticker']
The reason why you were getting AttributeError: 'DataFrame' object has no attribute 'Price'
is because the column label was being set to ' Price'
. So you would have had to access the column with df[' Price']
.
If you use
df = pd.read_csv('14_5.csv', skipinitialspace=True)
then you can access the first column with df['Price']
.
Upvotes: 2