在 csv 文件中将多个列表写为多行和多列 python

writing multiple lists as multiple row and column in a csv file python

嗨,我对将列表写成 CSV 格式有一些疑问

我有多个列表如下

head = ["name","roll_no","age"]
name = ["mohan","naveen","deepan"]
roll_no = [2087,2083,2090]
age = [23,23,24] 

我需要 CSV 文件输出为 头列表必须 header 列

name     roll_no    age

mohan     2087       23
naveen    2083       23
deepan    2090       24

这是我试过的代码:

    def write_csv(self, data, header_list):
        my_df = pd.DataFrame(data)
        my_df.to_csv(path_or_buf='my_csv.csv', index=True, header=header_list)
      

如有任何帮助,我们将不胜感激

尝试:

head = ["name", "roll_no", "age"]
name = ["mohan", "naveen", "deepan"]
roll_no = [2087, 2083, 2090]
age = [23, 23, 24]

df = pd.DataFrame(zip(name, roll_no, age), columns=head)
print(df)
df.to_csv("data.csv", index=False)

打印:

     name  roll_no  age
0   mohan     2087   23
1  naveen     2083   23
2  deepan     2090   24

并保存 data.csv(来自 LibreOffice 的屏幕截图):