In all the examples I have found, a column name is usually required to set it as the index
Instead of going into excel to add a column header, I was wondering if it's possible to set an empty header as the index. The column has all the values I want included, but lacks a column name:
My script is currently:
import pandas as pd
data = pd.read_csv('file.csv')
data
To create an index, from a column, in Pandas dataframe you use the set_index() method. For example, if you want the column “Year” to be index you type <code>df. set_index(“Year”)</code>. Now, the set_index() method will return the modified dataframe as a result.
Assign Column as Index using set_index() DataFrame. set_index() method to set a column as an index. In the below example, I am setting the column Courses as Index. When you do this, the column name is assigned as an index name and it will be removed from columns.
An unnamed column in pandas comes when you are reading the CSV file using it. Sometimes we require to drop columns in the dataset that we are not required. It not only saves memory but is also helpful in analyzing the data efficiently.
rename( columns={0 :'new column name'}, inplace=True ) . There is no need to use 'Unnamed: 0' , simply use the column number, which is 0 in this case and then supply the 'new column name' .
You could also just select the column by id with iloc
:
data = data.set_index(data.iloc[:, 0])
Or when you call pd.read_csv()
, specify index_col
:
data = pd.read_csv('path.csv', index_col=0)
You don't need to rename the first column in excel. It's as easy in pandas as well:
new_columns = data.columns.values
new_columns[0] = 'Month'
data.columns = new_columns
Afterwards, you can set the index:
data = data.set_index('Month')
You can do as follows:
import pandas as pd
data = pd.read_csv('file.csv',index_col=0)
data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With