Linked Questions

Popular Questions

Adding Header to a stacked CSV dataframe

Asked by At

I have a CSV file like attached. enter image description here

I'm trying to stack column B through G while using the following code:

import pandas as pd
data1 = pd.read_csv("C1_20psi.csv", skiprows=0, nrows=11, header = None, 
index_col = 0)
df1 = pd.DataFrame(data1)
df1.columns = ['trial 1','trial 2','trial 3','trial 4','trial 5','trial 6']
df1 = df1.stack()
print(df1)
df1.to_csv('reformat.csv')

This exported the csv I want as shownenter image description here but I'm not able to add different column headers back into the dataframe, which I will need in order to move columns from other dataframe to this one.

Can anyone help? Or is there a better way to stack data to achieve this format?

Related Questions