尝试使用pandas数据帧将数据写入特定列



`

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

`

例如,我想将贝多芬附加到古典专栏中,但我不知道如何将数据附加到特定专栏中。

1个错误示例:KeyError: "None of [Index(['Blues', 'Jazz', 'Rock', 'Country', 'Pop', 'Rap', 'Metal', 'Classical'], dtype='object')] are in the [columns]"

这就是除了之外的内容

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

相关内容

  • 没有找到相关文章

最新更新