Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向单个文件夹中的多个csv文件中添加仅包含一个值(例如:实验条件名称)的单列_Python_Pandas_Csv_Dataframe_Data Cleaning - Fatal编程技术网

Python 向单个文件夹中的多个csv文件中添加仅包含一个值(例如:实验条件名称)的单列

Python 向单个文件夹中的多个csv文件中添加仅包含一个值(例如:实验条件名称)的单列,python,pandas,csv,dataframe,data-cleaning,Python,Pandas,Csv,Dataframe,Data Cleaning,我有一个包含多个csv文件的文件夹,我想在文件夹中的所有csv文件中添加一列,其中包含实验条件的名称 import os, pandas as pd import csv file_list = list() os.chdir('.....name.../') g=[] for file in os.listdir(): if file.endswith('.csv'): df = pd.read_csv(file) df['Condition'] = "

我有一个包含多个csv文件的文件夹,我想在文件夹中的所有csv文件中添加一列,其中包含实验条件的名称

import os, pandas as pd
import csv
file_list = list()
os.chdir('.....name.../')
g=[]
for file in os.listdir():
    if file.endswith('.csv'):
        df = pd.read_csv(file)
        df['Condition'] = "After food"
        file_list.append(df)
        g.append(file_list)

如果我理解正确,您的问题是保存回csv文件。 如果要执行此操作,可以使用函数在循环函数中添加csv:

df.to_csv(file)

如果这不是你的意思,请告诉我,我会尽力帮助你。

如果我理解正确,你的问题是保存回csv文件。 如果要执行此操作,可以使用函数在循环函数中添加csv:

df.to_csv(file)

如果这不是你的意思,请告诉我,我会尽力帮助你。

假设你已经在正确的目录中,我们可以尝试以下方法:

PayCodes.csv has been altered
birmingham.csv has not met the condition
CF44.csv has not met the condition
DE11.csv has not met the condition
Dublin.csv has not met the condition
DY8.csv has not met the condition
首先,我们将所有csv文件传递到一个列表中

files = glob.glob('*.csv')
然后,我们可以使用方便的
try
except
语句遍历此列表以查找您的列

for file in files:
    df = pd.read_csv(file)
    try:
        df['Condition'] == "After food"
        # do something.
        df.to_csv(f'{file}.csv',index=False)
        print(f'{file} has been altered')
    except KeyError:
        print(f'{file} has not met the condition, therefore has not been changed.')
    except EmptyDataError:
        print(f"this {file} has no data to parse")
输出如下所示:

PayCodes.csv has been altered
birmingham.csv has not met the condition
CF44.csv has not met the condition
DE11.csv has not met the condition
Dublin.csv has not met the condition
DY8.csv has not met the condition

假设您已经在正确的目录中,我们可以尝试以下操作:

PayCodes.csv has been altered
birmingham.csv has not met the condition
CF44.csv has not met the condition
DE11.csv has not met the condition
Dublin.csv has not met the condition
DY8.csv has not met the condition
首先,我们将所有csv文件传递到一个列表中

files = glob.glob('*.csv')
然后,我们可以使用方便的
try
except
语句遍历此列表以查找您的列

for file in files:
    df = pd.read_csv(file)
    try:
        df['Condition'] == "After food"
        # do something.
        df.to_csv(f'{file}.csv',index=False)
        print(f'{file} has been altered')
    except KeyError:
        print(f'{file} has not met the condition, therefore has not been changed.')
    except EmptyDataError:
        print(f"this {file} has no data to parse")
输出如下所示:

PayCodes.csv has been altered
birmingham.csv has not met the condition
CF44.csv has not met the condition
DE11.csv has not met the condition
Dublin.csv has not met the condition
DY8.csv has not met the condition


你的问题是什么?什么不起作用?喜欢用户名btw=)@MyNameIsCaleb代码不起作用。你的问题是什么?什么不起作用?喜欢用户名btw=)@MyNameIsCaleb代码不起作用。@Gligo我试过了,得到的错误是EmptyDataError:没有要解析的列file@Gligo我试过了,我得到的错误为EmptyDataError:No columns to parse from file相同的错误EmptyDataError:No columns to parse fromfile@mashedpoteto你可以用另一个except语句来处理这个问题,请参见编辑后的答案。这是可行的,但我没有更改文件夹中的文件。它正在打印文件,文件已被更改,但是文件夹中的文件中添加了其他列。您需要更改路径请尝试打印
os.getcwd
,如果需要,请更改目录。文件很可能保存到与python程序相同的位置``import csv,os import pandas as pd os.chdir('/Desktop/folder')path=os.getcwd()+“/Desktop/folder/”files=glob.glob('*.csv')用于文件中的文件:df=pd.read_csv(file)try:df['Condition']==“After food”#do something.df.to_csv(f'{file}.csv',index=False)打印(f'{file}已被更改),但keyrerror:print(f'{file}未满足条件,因此未更改。“)但EmptyDataError:print除外(f“此{file}没有要分析的数据”)``相同的错误EmptyDataError:没有要从中解析的列file@mashedpoteto您可以使用另一个except语句来处理此问题,请参见编辑的答案。这是可行的,但我不会更改文件夹中的文件。它正在打印文件,文件已被更改,但文件夹中的文件中添加了其他列。您需要更改路径,请重试如果需要的话,点击os.getcwd并更改您的目录文件很可能保存到与python程序相同的位置``导入csv,os导入pandas as pd os.chdir('/Desktop/folder')PATH=os.getcwd()+“/Desktop/folder/”files=glob.glob('*.csv')对于文件中的文件:df=pd.read\u csv(file)try:df['Condition']==“food之后”#do something.df.to_csv(f'{file}.csv',index=False)打印(f'{file}已被更改),除了keyrerror:print(f'{file}未满足条件,因此未被更改。))除了EmptyDataError:print(f“此{file}没有要解析的数据”)```