将多个CSV文件中的相同行相加,但在pandas中的时间范围内通过配方进行分类

9bfwbjaz  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(42)

我有形式的任何地方20至300 CSV文件,每月我需要从文件中获得总体积[m3],但做的性质,需要的数据,我需要能够分类的CSV文件的总和下的食谱名称在一个月的时间范围内,时间范围可以修复与设置CSV文件在文件夹中的几个月,但需要读取配方名称并按总体积[m3]排序
目前我还没有找到一种方法来做到这一点,我目前所做的是读取文件中只有…

def read_csv_files(folder_path):
    # Get a list of all CSV files in the specified folder
    csv_files = [f for f in os.listdir(folder_path) if f.endswith(".csv")]

    for csv_file in csv_files:
        file_path = os.path.join(folder_path, csv_file)
        read_csv_file(file_path)

def read_csv_file(file_path):
    test = pd.read_csv(file_path, sep='";', header=[0, 13], nrows=0, engine='python', encoding='ANSI')
    print(test)

    
folder_path = "C:\\Users\\nw\\Documents\\Mini projects\\CSV"
read_csv_files(folder_path)

字符串
我的CSV文件是这样的:
配方;水批号;99订单号;3序列号;F1200 1406545公司;卡购买者;卡车号;工作名称;
批量启动; DT#2023-10-30-12:05:01批次结束;DT#2023-10-30-12:07:57混合时间[s];12 Act. WCT;0.0 Set p. WCT; 0.0 Total volume[m ];0.1 Total weight[kg];2.2 Concrete type no; Strength class; Consistency range; Largest grain; Cement type; Additive; Admixture; Last batch in order;TRUE Manuel activated;TRUE Alarms:ERROR_PICCLEValve_PosMixerNotOn PICRER_bDOSING_TOLERANCE_WATER1 ExtraText;
我试着把它添加到一个列表/数组中,就像在这里的另一篇文章中发现的那样,但是因为我的数据是以一种不同于普通csv的方式生成的,所以我真的没有得到任何东西
我已经厌倦了遵循一个例子形式的另一个职位在这里,但做我的csv文件是如何使它不可能的方式。
the output of my print(df)Empty DataFrame Columns:[(Recipe;water,Total volume[m³];0.1)] Index:[] Empty DataFrame Columns:[(Recipe;Gulv og konstruktioner udvendig kl. M C30/37,Total volume[m³];0.35)] Index:[]

6xfqseft

6xfqseft1#

如果没有数据样本,很难提供帮助。
首先,你需要创建一个新的月份列。我假设你从“Batch Start”中获取它,格式是字符串:

df['month'] = df['Batch start'].str.split('-')[1]

字符串
现在你可以使用groupby来计算配方和月份的总和:

df.groupby(['Recipe', 'Month']).sum()


告诉我这对你是否有效

相关问题