[Pandas] If the first row data is in the header in DataFrame

(It is a story assuming Jupyter Notebook.)

What to do if the data in the first row of the DataFrame gets into the header (columns) for some reason.

It's easy to read again or rebuild the DataFrame, If you want to fix it with code somehow, I summarized how to fix it with DataFrame.

For example, consider the case where the data in the first line is originally included in the header part as shown below.

a 0
0 b 1
1 c 2
2 d 3
3 e 4

I want to insert [a, 0] in the header at the position of index = 0, but pandas doesn't seem to have such a function, so process it as follows.

df = df.shift()                #Shift the row data downward one by one
df.iloc[0] = df.columns.values #index=Originally substitute the data of the first line at the position of 0
df.columns = ["col1", "col2"]  #Set the original column name

(result)

col1 col2
0 a 0
1 b 1
2 c 2
3 d 3

that's all.

Recommended Posts

[Pandas] If the first row data is in the header in DataFrame
Is there NaN in the pandas DataFrame?
Check if the expected column exists in Pandas DataFrame
The first step to log analysis (how to format and put log data in Pandas)
Check if it is Unix in the scripting language
Determine if an attribute is defined in the object
Check if it is Unix in the scripting language
How to find out if there is an arbitrary value in "somewhere" of pandas DataFrame
Put the lists together in pandas to make a DataFrame
The minimum methods to remember when aggregating data in Pandas
python / pandas / dataframe / How to get the simplest row / column / index / column
Check if the configuration file is read in an easy-to-understand manner
Delete a particular character in Python if it is the last
What to do if "Unnamed: 0" is added in to_csv-> read_csv in pandas
Browse .loc and .iloc at the same time in pandas DataFrame
[Golang] Check if a specific character string is included in the character string
Determine if the string is formatable
Determine if the library is installed.
Check the data summary in CASTable
The first step in Python Matplotlib
If the conda environment is strange, let's put it back in mac
If you use Pandas' Plot function in Python, it is really seamless from data processing to graph creation
[Python] What is pandas Series and DataFrame?
Determine if the gold coin is genuine
Get the top nth values in Pandas
Check if the URL exists in Python
What is "mahjong" in the Python library? ??
How to reassign index in pandas dataframe
[Pandas] Expand the character string to DataFrame
I saved the scraped data in CSV!
When the target is Ubuntu 16.04 in Ansible
MongoDB for the first time in Python
Print the include header path in clang
RDS data via stepping stones in Pandas
Working with 3D data structures in pandas
The date is displayed incorrectly in matplotlib.
Even if the development language is changed to python3 in Cloud9, version 2 is displayed in python --version
[pandas] When specifying the default Index label in the at method, "" is not required
If branch depending on whether there is a specific element in the list
Find the index of items that match the conditions in the pandas data frame / series
Why Docker is so popular. What is Docker in the first place? How to use
How to replace with Pandas DataFrame, which is useful for data analysis (easy)
[For beginners] Unexpected behavior if "\" is included when setting the path in Python