Home > OS >  How to add custom header to my dataframe | pandas
How to add custom header to my dataframe | pandas

Time:06-25

I have been searching over the internet but could not find proper details

How to add my custom header to my dataframe

Data :

                                       0
0              $5.95,Belgian Waffles,650
1   $7.95,Strawberry Belgian Waffles,900
2  $8.95,Berry-Berry Belgian Waffles,900
3                 $4.50,French Toast,600
4          $6.95,Homestyle Breakfast,950

List: final_data_set -> This is list which contains data and got above result

Logic

headerList = [ 'price','name','calories' ]
df = pd.DataFrame(final_data_set)
print(df)

Required :

Need to remove vertical numbers : 0 1 2 3 4 and header which is 0

Required to add header fields as : price','name','calories

Need to remove frontend spaces as well

Expected output :

price,name,calories
$5.95,Belgian Waffles,650
$7.95,Strawberry Belgian Waffles,900
$8.95,Berry-Berry Belgian Waffles,900
$4.50,French Toast,600
$6.95,Homestyle Breakfast,950

CodePudding user response:

I think the following will help you

df = pd.DataFrame(final_data_set, columns=headerList)

CodePudding user response:

We should split the data into columns and replace the columns in the dataframe.

df = df["0"].str.split(",", -1, expand=True)
df.columns=["price","name","calories"]
  • Related