[英]python storing a dataframe as value associated with key in dictionary
我正在嘗試將數據框存儲為與字典中的鍵相關聯的值。 當我為鍵打印字典時,我只會得到列名。
keys=['A','B','C']
temp_list= {}
temp_list=temp_list.fromkeys(keys,list)
for key in keys:
temp_list[key]=[]
for key in keys:
A=pd.DataFrame(np.random.randn(10, 5),columns=['a', 'b', 'c', 'd', 'e'])
temp_list[key].extend(A)
#print(temp_list[key])
for key in keys:
B=temp_list[key]
print(B)
這是我得到的輸出是:
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
['a', 'b', 'c', 'd', 'e']
我是python的新手。 是否可以將數據框作為值存儲在字典中? 如果不可能,如何在數據框和密鑰之間創建關聯?
怎么樣:
keys=['a','b','c','d','e']
A=pd.DataFrame(np.random.randn(10, 5),columns=['a', 'b', 'c', 'd', 'e'])
DICT_MAP={}
for key in keys:
DICT_MAP[key]=pd.Series(A[key])
您現在可以將鍵映射到DataFrame:DICT_MAP ['a'] = ..SeriesDataFrame
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.