【问题标题】:Iterate over files in a folder to populate a dataframe (python)迭代文件夹中的文件以填充数据框(python)
【发布时间】:2021-12-24 08:54:20
【问题描述】:

我正在从一个文件夹中的不同数据文件编译天气数据。 Stations.csv - 这些数据文件被定义为此 csv 中的位置点。 temperature.csv - 需要在此数据框中编译天气数据。 迭代循环的问题:

stations = pd.read_csv('SMHIstationset.csv', index_col='Unnamed: 0') 车站.head()

stations dataframe

temperature = pd.read_csv('SMHITemp.csv', index_col='Unnamed: 0')
temperature.head()

Compile data from folder files in this 'temperature' dataframe

我已经使用这个循环来识别数据文件夹中的文件;

x = []
filelist= os.listdir("C:\\Users\\SEOP24209\\Desktop\\API\\")
for filename in filelist:
    if filename.endswith(".xlsx"):
        x.append(filename)

问题在于继续以下部分的结果以形成循环以迭代文件夹中的文件以填充数据框(温度)。 第 1 部分:

x = stations.loc[stations['Id'] == 188790, 'Name']
#y = (x[2])
print (type(x))
print(x)

输出: 2 阿比斯库奥特 名称:名称,dtype:对象

第 2 部分:

city = 'Abisko Aut'
temperature[city]=df['T2M']
temperature.rename(columns={'index':'Timestamp'}, inplace=True)
temperature.head()

当我读取文件夹中的每个文件时,我希望上述部分 (1&2) 处于循环状态。如果您了解我如何将变量“city”的输入作为系列第 1 部分的输出,请指导。

【问题讨论】:

    标签: python loops directory series folderbrowserdialog


    【解决方案1】:

    使用.tolist()将系列转换为列表,然后获取索引0处的数据以获取馈送到变量城市的结果,从而完成循环。

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2021-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多