How we can add a row before the dataframe in pandas
I have a CSV file used as a pandas data frame, now I only need to insert a dummy row before the data frame starts.
In pandas add row to dataframe we can use:
df.loc[len(df)] = [4, 'd']
#4 is the value of column 'A'
Consider the following examples: 'label' in A1, 'time' in A2, and data in A3, B3, and C3. To insert a dummy row before the data frame begins use the snippet below.
Code:
#renaming Index to time
df.index.name = 'time'
df.index +=1
#resets and convert the old index
df.reset_index(inplace=True)
#it will create a multi-index where the first is an empty string and the second level is original column names
df.columns = pd.MultiIndex.from_tuples(zip([' ']*256, df))
#saves the dataframe to csv
df.to_csv(values["-OUTPUT_PATH-"]+'/converted.csv', index=False)
This above snippet will do the required work.