'Importing a data frame from CSV file using Pandas with column name having spaces
I am trying to import a data frame from a .csv
file which contains Per Capita Income. Moreover, in the above mentioned file the column name is Per Capita Income (US$), however when I try to import it with the same name it gives me a syntax error, which is quite obvious. However, when I remove spaces from the column name in the subject .csv
file it works fine - as per the below codedf.percapita
; as I have changed it from Per Capita Income (US$) to percapita. What could I possibly do to use the same column name as mentioned in the given file to import the required data frame.
import numpy as np
import matplotlib.pyplot as plt
from sklearn import linear_model
df = pd.read_csv("/xxxx/xxxx/Desktop/Python/Machine Learning/Linear Regression/PortugalIncome.csv")
reg = linear_model.LinearRegression()
reg.fit(df[['year']], df.percapita)
reg.predict(np.array([2017]).reshape(1,1))
y = reg.coef_*2016+reg.intercept_
print(y)
Solution 1:[1]
Possibly access the column by:
df["Per Capita Income (US$)"]
instead of df.percapita
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |