所以我正在做一個時間序列/LSTM 作業,我有一個股票資料集:https : //www.kaggle.com/camnugent/sandp500
資料集中有 500 家公司,每個公司都有一組行,我想要的是將這些公司添加到字典中,并將鍵設定為每個公司的名稱。
這是我目前所擁有的:
dataframe = pd.read_csv('all_stocks_5yr.csv', parse_dates=['date'])
dataframe['date'] = pd.to_datetime(dataframe['date'])
grouped_df = dataframe.groupby('Name')
for i in grouped_df:
df_dict = grouped_df[i].to_dict
uj5u.com熱心網友回復:
這將解決您的問題:
gp = dataframe.groupby("Name")
my_dict = {} # This is the output you want
for record in gp: # record is a tuple containing the elements of a row
if record[0] in my_dict: # record[0] will give the name of the company
my_dict[record[0]].append(record)
else:
my_dict[record[0]] = [record]
print(my_dict)
處理此問題的另一種方法是迭代資料幀:
my_dict = {}
for index, record in dataframe.iterrows():
if record['Name'] in my_dict:
my_dict[record['Name']].append(record)
else:
my_dict[record['Name']] = [record]
print(my_dict)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/365715.html
