0

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
1
  • How do you read your data? It looks like the columns should have been split already at the read_csv stage (or similar), but were not. Different options to the reading function should fix that. Fix the reading properly to save you time for the next time you need it, instead of splitting after the fact (unless absolutely needed) Commented Jun 25, 2022 at 10:28

2 Answers 2

3

I think the following will help you

df = pd.DataFrame(final_data_set, columns=headerList)
Sign up to request clarification or add additional context in comments.

1 Comment

Apparently, final_data_set is a list (1 column) and then it won't work to pass three headers.
1

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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.