aampere
aampere

Reputation: 280

Join row contents element-wise in pandas

Sometimes my csv's have more than one header row, all of which are important. For instance, the second header row might contain the scientific unit of the values in the column. I need to combine the header rows into one header row so that I can do operations and plots on the numerical data. I want to be able to do this for the first n lines in the csv, or rows in the dataframe, where I specify n.

enter image description here

Alternate suggestions on how to deal with this problem welcome. Editing each csv is not viable. pandas beginner here, but not new to programming.

Upvotes: 2

Views: 143

Answers (1)

Umar.H
Umar.H

Reputation: 23099

I usually handle data like this as follows :

just creating some dummy data for you, please prepare this for SO in future, it really helps others answer your question easily. (also incase anyone has a better answer)!

pore_throat = ['(nm)',21638,20542,19431.2,18262.85]
injection_pres = ['(psi)',4.93,5.19,5.49,5.82]
df = pd.DataFrame({'pore throat radius' : pore_throat,
             'Injection Pressure' : injection_pres})

assuming your data is called file:

df_cols = pd.read_csv(file,nrows=1)

cols = df_cols.columns + ' ' + df_cols.iloc[0]
df = pd.read_csv(file,skiprows=1) # skip the string row so the dtypes are numbers/floats.
df.columns = cols
print(df)
pore throat radius (nm)     Injection Pressure (psi)
1   21638.00    4.93
2   20542.00    5.19
3   19431.20    5.49
4   18262.85    5.82

Upvotes: 2

Related Questions