为什么我的数据没有完全连接?

问题描述 投票:0回答:1

我有 3 个不同太阳能电池板系统每 2 分钟(时间戳列)的每日电量值。 (每次产生的电力的时间戳列。)每个 .csv 工作表都有 1 天的数据,所以我的文件有 x 18 个月的数据。

我想获得每个系统在每个时间戳的平均发电量。最终结果应该是发电量与时间戳的关系图。

我的问题是,当我使用 .shape() 检查文件是否已加载到数据框中时,打印的结果太小 - (588,8)。返回 1 个月的 Excel 工作表串联 (4536,8)。不知道哪里出了问题。

我尝试了下面的代码。目的是将所有日常 .csv 文件连接到一个数据帧中,然后进行分组。太阳能电池板系统和时间戳,然后取平均值。

import pandas as pd
import os

excel_dir = '/kaggle/input/pv-output-1/PV 2122'

first_file = '/kaggle/input/pv-output-1/PV 2122/1 Apr 2021.csv'
PV_2122 = pd.read_csv(first_file)

for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        name = str(os.path.join(excel_dir, first_file))
    if name.endswith('.csv'):
        file_path = pd.read_csv(name)
        PV_2122 = pd.concat([file_path, PV_2122], ignore_index = True)

PV_2122.shape()
string graph concatenation shapes
1个回答
0
投票

您的代码中似乎存在一些问题。让我帮你纠正它们:

1.定义名称的行使用first_file而不是文件名。 2.读取第一个文件的行(PV_2122 = pd.read_csv(first_file))位于循环之外,因此它会多次读取第一个文件。 3.pd.concat中的ignore_index参数应设置为True(不是true)。 4.您尝试将 shape 作为方法调用 (PV_2122.shape()),但它是一个属性,因此您应该使用 PV_2122.shape。 这是更正后的代码:

import pandas as pd
import os
excel_dir = '/kaggle/input/pv-output-1/PV 2122'

first_file = '/kaggle/input/pv-output-1/PV 2122/1 Apr 2021.csv'
PV_2122 = pd.read_csv(first_file)

for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        name = str(os.path.join(excel_dir, filename))  # Corrected from first_file to filename
        if name.endswith('.csv'):
           file_path = pd.read_csv(name)
           PV_2122 = pd.concat([file_path, PV_2122], ignore_index=True)  # Corrected ignore_index to True

print(PV_2122.shape)  # Corrected from PV_2122.shape() to PV_2122.shape

通过这些更正,您的代码应该正确地将所有每日 CSV 文件连接到一个数据帧中,并且您可以继续进行分析以计算每个系统在每个时间戳产生的平均电量。

© www.soinside.com 2019 - 2024. All rights reserved.