I have a very simple table in Excel that I'm trying to read into a DataFrame
Code:
from pandas import DataFrame, Series
import pandas as pd
df = pd.read_excel('params.xlsx', header=[0,1], index_col=None)
This results in the following DataFrame:
I didn't expect param1.key to become the index, especially after having set index_col=None
. Is there a way to get the data into a DataFrame with a generated index instead of the data from the first column?
Update — here's what happens when you try reset_index()
to resolve the issue:
Version info:
An index column is also added to an Excel worksheet when you load it. To open a query, locate one previously loaded from the Power Query Editor, select a cell in the data, and then select Query > Edit. For more information see Create, load, or edit a query in Excel (Power Query). Select Add Column > Index Column.
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.
We can set a specific column or multiple columns as an index in pandas DataFrame. Create a list of column labels to be used to set an index. We need to pass the column or list of column labels as input to the DataFrame. set_index() function to set it as an index of DataFrame.
It seems like a bug. You can get a column out of your index by simply doing:
df['columnName'] = df.index
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