Turn the column headers into the first row and row headers into the first column in Pandas dataframe

convert column header to row pandas
pandas read_csv header first row
replace header with first row pandas
how to make the first row the column names in python
convert row to column header for pandas dataframe
pandas header
add column names to dataframe pandas
turn row into header pandas

I have a dataframe that looks like so:

123  345   456   789
987  876   765   543
...  ...   ...   ...

But the top row and leftmost column are taken as headers when they are actually values. Is there anyway to shift them down/right and replace them with the default index?

EDIT: I have already considered setting header=None, but it is not an option. The dataframe was created via a read_excel, but many parts of the program already use .loc and such and directly reference the header names that are to be dropped.

for your solution, you can just shift it. But if you are reading the data from any csv file, while reading you can take considerations of not taking header(header = None)

    345 456 789
123         
987 876 765 543

df.reset_index().T.reset_index().T

Out:

         0  1   2   3
index   123 345 456 789
0       987 876 765 543


pd.read_csv('data.csv',header=None)

Out:

    0   1   2   3
0   123 345 456 789
1   987 876 765 543

Python Pandas Replacing Header with Top Row, First, you have to grab the first row for the header then take the data less the header row after that set the header row as the df header. If you have imported a CSV file into your notebook and use Pandas to view the dataframe you might find that the header of your spreadsheet is actually your first row of the dataframe. You can’t run analysis when your dataframe looks like the above. What you need is the first row to be your header and there is a simple way to do this in your notebook.

Use parameter index_col=[0], by default first row is converted to columns names, so no parameter for it is necessary:

import pandas as pd

temp=u"""123;345;456;789
987;876;765;543"""
#after testing replace 'pd.compat.StringIO(temp)' to 'filename.csv'
df = pd.read_csv(pd.compat.StringIO(temp), sep=";", index_col=[0])

print (df)
     345  456  789
123               
987  876  765  543

If input data is DataFrame with no header:

print (df)
     0    1    2    3
0  123  345  456  789
1  987  876  765  543

#set first row to columns
df.columns = df.iloc[0]
#remove first row from data and remove columns name
df = df.iloc[1:].rename_axis(None, axis=1)
#set index by first column
df = df.set_index(df.columns[0])
print (df)
     345  456  789
123               
987  876  765  543

If same types of values in data is possible use numpy with indexing:

arr = df.values
df = pd.DataFrame(arr[1:,1:], index=arr[1:,0], columns=arr[0,1:])
df.index.name = arr[0,0]
print (df)
     345  456  789
123               
987  876  765  543

Rename Column Headers In pandas, Rename Column Headers In pandas. 20 Dec 2017. Originally from rgalbo on StackOverflow. Replace the header value with the first row's values. # Create a new variable called 'header' from the first row of the dataset header = df.iloc[0]. Stack/ Unstack won't work until you have the desired column in your row/ column indexes. e.g. In simple words, Stack/ Unstack will bring the lowest level of column index to the lowest level of row index and vice versa. So in your case, you can achieve the same results with stack/unstack by

There seems to be an issue with the creation of the dataframe. How is the dataframe created? You most likely can solve your issue right with the creation

If that, however, is not an option, try the following: pandas.DataFrame.reset_index() is what you want. As for the column names, just add them as a regular row using pandas.DataFrame.append() with df.columns as an argument (where df is your dataframe) and rename the columns after.

How to make the first row in your spreadsheet or dataframe, What you need is the first row to be your header and there is a simple way to do this in #Convert to a DataFrame and render. import pandas as pd #Save the set the header row as the df header df.columns = new_header For the dataframe DF, the following line of code will set the first row as the column names of the dataframe: DF.columns = DF.iloc[0] share | improve this answer | follow |

How to add header row to a Pandas DataFrame, We can use names directly in the Cov = pd.read_csv( "path/to/file.csv", sep='\t', to Convert DataFrame Column to one row to Pandas DataFrame  In order to convert a column to row name/index in dataframe, Pandas has a built-in function Pivot. Now, let’s say we want Result to be the rows/index, and columns be name in our dataframe, to achieve this pandas has provided a method called Pivot.

How to convert a Pandas DataFrame row to column headers in Python, Converting a Pandas DataFrame row to column headers takes the value of each element in the row and sets it as the header of that column. Use pandas. Please do as follows to convert column headers to row headers in Excel. 1. Please select the table data which you need to convert the column headers to rows, and then copy the table by pressing the Ctrl + C keys simultaneously. 2. Right click a blank cell where you want to place the converted table, then click Paste Special > Paste Special. See

Convert a column to row name/index in Pandas, handle data and its transformation. Let's see how can we convert a column to row name/index in Pandas. Create a dataframe first with dict of lists. filter_none. Your actual column headers are in the second row. How can you make them the real column headers? Drag a Sample tool into the workflow. In the configuration window, choose “Skip 1st N records”. In the “N =” box, set N = 1 (as in this case, our data is in the second row, so we want to skip the first row). Now our data looks like this –

Comments
  • How was this dataframe created in this obviously wrong manner? Did you import the data into the dataframe? Do you have influence on how it is created? Data without headers should be treated as such already when put into a dataframe.
  • This should be addressed when reading in the file pd.read_csv(..., header=None). That said, resetting the index is easy df.reset_index(). The columns are a bit annoying df.T.reset_index().T. But if you really need both, then you need to address what goes in the resulting first position. Meaning, the top left corner of the dataframe won't have a value. I think you need an minimal reproducible example
  • sry, that was a mistake :-| , i just edited it. it seems OP is asking to read the dataframe without mentioning first row data as columns( he might be reading from numpy array ) @jezrael