Reputation: 83
I have the following csv file which is displayed in excel as the following:
A B C D E
1 x
2
3 a b c d e
4
5
where x is a value I need to save as a variable (specifically to add a new column 'f' in row 3 with the x value for all rows) and where row 3 (a,b,c,d,e) is the actual header row to be used.
I know you set the header during the .read_csv portion of the code but would I need to set the header = 0, save the variable, and then set header = 3? If so, how do I adjust the header after reading in the csv. Thank you in advance.
Upvotes: 0
Views: 89
Reputation: 2248
After reading the file normally and adding the value "x" to row 3. You can create a new df with that row's values as column names and ignoring the rows before the third row
headers = df.iloc[2]
new_df = pd.DataFrame(df.values[3:], columns=headers)
Upvotes: 1