2

`

import pandas as pd
import numpy as np
import csv

header = ["Blues", "Jazz", "Rock", "Country", "Pop", "Rap", "Metal", "Classical"]

with open("music.csv", "w") as SectorCSV:
    csvwriter = csv.writer(SectorCSV, lineterminator = "\n")
    print("Creating Sector.csv")
    csvwriter.writerow(header)

df = ["Beethoven", "Mozart", "Adele", "Beyonce", "Nirvana", "Eminem", "Metallica", "Bach"]
df = pd.DataFrame(df)
df.to_csv("Sector.csv", index=False, columns=header, mode="a")

`

For example I want to append Beethoven into the classical column but I can't figure out how to append data to a specific column.

1 example of an error: KeyError: "None of [Index(['Blues', 'Jazz', 'Rock', 'Country', 'Pop', 'Rap', 'Metal', 'Classical'], dtype='object')] are in the [columns]"

1 Answer 1

1

I'm this is what you are excepting

df = pd.DataFrame()
df['columnname'] = ["Beethoven", "Mozart", "Adele", "Beyonce", "Nirvana", "Eminem", "Metallica", "Bach"]
df.to_csv("Sector.csv", index=False, columns=header, mode="a")
Sign up to request clarification or add additional context in comments.

5 Comments

shouldn't be? df['Classical']=...
yes you can change the column name based on header
It still gives me this error: KeyError: "None of [Index(['Blues', 'Jazz', 'Rock', 'Country', 'Pop', 'Rap', 'Metal', 'Classical'], dtype='object')] are in the [columns]"
All I want is to append data to a column under a specific header
or you can try this { df=pd.read_csv("music.csv") df1 = pd.DataFrame() df1['median_house_value'] = ["Beethoven", "Mozart", "Adele", "Beyonce", "Nirvana", "Eminem", "Metallica", "Bach"] df3=pd.concat([df,df1],ignore_index=True) }

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.