1
我無法從幾個.csv文件中將數據獲取到單個數組中。我可以從.csv文件中獲取所有數據,但我無法將所有數據都轉換爲簡單的numpy數組。每個.csv文件的名稱對我來說都很重要,所以最後我希望有一個Pandas DataFrame,其中包含以.csv文件的初始名稱標記的列。將多個.csv文件中的數據導入到單個數據框中
import glob
import numpy as np
import pandas as pd
files = glob.glob("*.csv")
temp_dict = {}
wind_dict = {}
for file in files:
data = pd.read_csv(file)
temp_dict[file[:-4]] = data['HLY-TEMP-NORMAL'].values
wind_dict[file[:-4]] = data['HLY-WIND-AVGSPD'].values
temp = []
wind = []
name = []
for word in temp_dict:
name.append(word)
temp.append(temp_dict[word])
for word in wind_dict:
wind.append(wind_dict[word])
temp = np.array(temp)
wind = np.array(wind)
當我打印溫度或風我得到的是這樣的:
[array([ 32.1, 31.1, 30.3, ..., 34.9, 33.9, 32.9])
array([ 17.3, 17.2, 17.2, ..., 17.5, 17.5, 17.2])
array([ 41.8, 41.1, 40.6, ..., 44.3, 43.4, 42.6])
...
array([ 32.5, 32.2, 31.9, ..., 34.8, 34.1, 33.7])]
當我真正想要的是:
[[ 32.1, 31.1, 30.3, ..., 34.9, 33.9, 32.9]
[ 17.3, 17.2, 17.2, ..., 17.5, 17.5, 17.2]
[ 41.8, 41.1, 40.6, ..., 44.3, 43.4, 42.6]
...
[ 32.5, 32.2, 31.9, ..., 34.8, 34.1, 33.7]]
這並不工作,但爲目標我代碼:
df = pd.DataFrame(temp, columns=name)
而當我嘗試t o使用Pandas的DataFrame,每一行都是它自己的數組,因爲它認爲每一行只有元素,所以它沒有幫助。我知道問題是與「陣列(...)」我只是不知道如何擺脫它。提前感謝您的寶貴時間和考慮。
什麼是'temp'轉換爲'array'前? – jezrael
你可以添加一些樣品嗎? – jezrael