简体   繁体   English

将带有每列值的标题行添加到多个 CSV 文件

[英]Adding a header row with values for each column to multiple CSV files

I have multiple CSV files in one directory but with no headers.我在一个目录中有多个 CSV 文件,但没有标题。 I'm looking for a robust way to add same headers to all files in my directory at once.我正在寻找一种强大的方法来一次向我目录中的所有文件添加相同的标头。

Sample.csv:示例.csv:

 John Doe    Guitar    4 units

Desired output after adding headers 'name', 'product', 'quantity':添加标题“名称”、“产品”、“数量”后所需的输出:

 name       product    quantity 
John Doe    Guitar     4 units

so far I found a way to add headers into a single file with pandas:到目前为止,我找到了一种使用 Pandas 将标题添加到单个文件中的方法:

from pandas import read_csv      
df = read_csv('/path/to/my/file/Sample.csv')
df.columns = ['name', 'product', 'quantity']
df.to_csv('/path/to/my/file/output.csv')

now I guess I would have to add a loop that would read all files in my directory and add desired header row into each.现在我想我必须添加一个循环来读取我目录中的所有文件并将所需的标题行添加到每个文件中。 Could someone help me with this step or suggest some other easier approach if possible?如果可能的话,有人可以帮助我完成这一步或建议一些其他更简单的方法吗? Thank you in advance.先感谢您。

attempting to add loop but it throws an error message:试图添加循环,但它抛出一条错误消息:

import pandas as pd 
import os
import glob
from pandas import read_csv 
path = '/path/to/my/files/'
filelist = glob.glob(path + "/*.csv")
frame = pd.DataFrame()
list = []
frame = pd.DataFrame()
#whenever i run the below line it throws this error ->   IndentationError: expected an indented block
for file in filelist:
    df2 = pd.read_csv(path+file)
    df2.columns = ['name', 'product', 'qunatity']
    list.append(df2)
frame = pd.concat(list)

Read_csv has a names parameter that you can use for columns. Read_csv有一个可以用于列的名称参数。

If you want to add the same header into every csv you read.如果您想将相同的标题添加到您阅读的每个 csv 中。 You can just pass the columns into the names parameter when you read the .csv files.您可以在读取 .csv 文件时将列传递给 names 参数。


df = pd.read_csv('test_.csv', names = ['name', 'product', 'quantity'])

Editing your code.编辑您的代码。 You are doing too much here you don't need to create a dataframe in the beginning.你在这里做的太多了,你不需要在一开始就创建一个数据框。 Also do not call your list "list" list is a special word in python.也不要将您的列表称为“列表”列表是 python 中的一个特殊词。

You also do not need to add the path to the file, your glob list will already have the full path you need.您也不需要添加文件路径,您的 glob 列表已经包含您需要的完整路径。

In regards to the indentation error.关于缩进错误。 I would make sure you are using consistent indentations, sometimes that happens if you use spaces to indent for one line and a tab for another.我会确保您使用一致的缩进,有时如果您使用空格缩进一行而使用制表符缩进另一行,有时会发生这种情况。 I would simply delete the indentation and add it back the same way.我会简单地删除缩进并以相同的方式将其添加回来。

import pandas as pd 
import os
import glob
from pandas import read_csv 
path = '/path/to/my/files/'
filelist = glob.glob(path + "/*.csv")
df_list = []
for file in filelist:
# you also dont need to add path, the glob should already have the full path
    df2 = read_csv(file,names=['name', 'product', 'quantity'])
    ## save out files
    df2.to_csv(file,index=False)
    df_list.append(df2)
frame = pd.concat(df_list)
frame = pd.concat(df_list)

Also there is an even easier way to to this with list comprehension.还有一种更简单的方法可以通过列表理解来实现。 See below.见下文。

import pandas as pd 
import os
import glob
path = '/path/to/my/files/'
filelist = glob.glob(path + "/*.csv")
frame = pd.concat([pd.read_csv(file,names=['name', 'product', 'quantity']) for file in filelist])

暂无
暂无

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

相关问题 将 header 添加到多个 csv 文件 - Adding a header to multiple csv files 根据列值连接多个 CSV 文件,但多个 csv 文件具有相同的 header 但顺序不同 - Concatenating multiple CSV files based on column values,but the multiple csv files have the same header but vary in order 如何在某些列值上按行过滤多个csv文件 - How to filter multiple csv files by row on certain column values 当行内容是相关键的键值(每列的标题)时,如何用python在csv中编写嵌套字典? - How to write nested dictionary in csv with python when the row contents are key values of related key (the header of each column)? CSV合并并向每个文件的每一列添加新行 - CSV merging and adding new row to each column in each file Python 从多个 CSV 文件中读取数据并将每个文件添加到新列 - Python Reading data from multiple CSV files and adding each file to a new column 根据一列内的匹配项合并2个CSV文件,而不管标题行 - Combine 2 CSV files based on a match within a column disregarding the header row Pandas 在合并多个 CSV 文件后向输出文件添加标题 - Pandas adding header to the output file after merging multiple CSV files Python:当一个键有多个值时,如何将字典写入csv文件,每个键是一个新行,但每个值是一个新列? - Python: How to write dictionary to csv file when one key has multiple values, each key is a new row, but each value is a new column? 在python中向csv添加列标题 - Adding a column header to a csv in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM