简体   繁体   English

For 循环根据 csv python 中的列名创建标题

[英]For loop to create headers based on column names in csv python

I am trying to do a for loop to append my headers of an excel to separate variables as header1, 2, 3 and so on.我正在尝试对 append 我的 excel 的标头执行 for 循环,以将变量分隔为 header1、2、3 等。 For example, In the attached image my excel has 5 columns and I need my results to be as below, could someone please help, Thanks in advance例如,在附图中,我的 excel 有 5 列,我需要我的结果如下,有人可以帮忙吗,在此先感谢

header1 = 'Statistics'
header2 = 'Machine Learning'
header3 = 'R Language'
header4 = 'NLP'
header5 = 'Data Engineering'

Attached is the image of my excel header附上我的excel header的图片

在此处输入图像描述

I am not sure what you are looking for.我不确定你在找什么。

But to me it sounds like you want to retrieve the column labels of a pandas dataframe.但对我来说,听起来你想检索 pandas dataframe 的列标签。 You can do so like this:你可以这样做:

import pandas as pd

df = pd.read_csv("/path/to/your/file/excel.csv", skiprows = 1)
header1, header2, header3, header4, header5 = df.columns.values

We skip the first row because, If I remember correctly, when reading .csv files the first row is a default csv thing.我们跳过第一行是因为,如果我没记错的话,在读取.csv文件时,第一行是默认的 csv 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM