(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