簡體   English   中英

如何創建列表列表,其中子列表是每列的列值

[英]How to create a list of lists where the sub-lists are the column values for each column

我是蟒蛇和熊貓的新手。 我正在嘗試將包含一列的所有行放入列表的每個索引中。

這是我的代碼。

result = pd.concat(result, axis=1).fillna(method='bfill').fillna(method='ffill')

印刷的輸出就像

           1           2
0       0.57  739.679993
1       0.57  739.679993
2       0.57  739.679993
3       0.57  739.679993
4       0.57  739.679993
5       0.57  739.679993
6       0.57  739.679993
7       0.57  739.679993
8       0.57  739.679993
9       0.57  739.679993
10      0.57  739.679993
11      0.57  739.679993
12      0.57  739.679993
13      0.57  739.679993
14      0.57  739.679993
15      0.57  739.679993
16      0.57  739.679993
17      0.57  739.679993
18      0.57  739.679993
19      0.57  739.679993
20      0.57  739.679993
21      0.57  739.679993
22      0.57  739.679993
23      0.57  739.679993
24      0.57  739.679993
25      0.57  739.679993
26      0.57  739.679993
27      0.57  739.679993
28      0.57  739.679993
29      0.57  739.679993
...      ...         ...
121571  0.72  738.000000
121572  0.72  738.000000
121573  0.72  738.000000
121574  0.72  738.000000
121575  0.72  738.000000
121576  0.72  738.000000
121577  0.72  738.000000
121578  0.72  738.000000
121579  0.72  738.000000
121580  0.72  738.000000
121581  0.72  738.000000
121582  0.72  738.000000
121583  0.72  738.000000
121584  0.72  738.000000
121585  0.72  738.000000
121586  0.72  738.000000
121587  0.72  738.000000
121588  0.72  738.000000
121589  0.72  738.000000
121590  0.72  738.000000
121591  0.72  738.000000
121592  0.72  738.000000
121593  0.72  738.000000
121594  0.72  738.000000
121595  0.72  738.000000
121596  0.72  738.000000
121597  0.72  738.000000
121598  0.72  738.000000
121599  0.72  738.000000
121600  0.72  738.000000

我想將每列的整行放到列表中。 例如,有一個列表result_temp = []

for i in range(0, lenghofColumn):
  result_temp.append(result[:,i])

但是,它給出了一個錯誤: Error: TypeError: unhashable type

 File "public/make_bokeh.py", line 49, in <module>
      real_result.append(result_temp[:,i])

有沒有辦法做到這一點......?

請幫幫我...

提前致謝。

最好的是轉置值,轉換為numpy array ,最后是list s:

result_temp = result.T.values.tolist()

我認為您可以使用list comprehension ,其中按列名稱循環,並按[]按名稱選擇每列,並轉換為list

result_temp = [result[x].tolist() for x in result.columns]

它與:

result_temp = []
for x in result.columns:
    result_temp.append(result[x].tolist())

如果想要使用你的代碼需要DataFrame.iloc來按位置選擇列:

result_temp = []
lenghofColumn = len(result.columns)
for i in range(0, lenghofColumn):
  result_temp.append(result.iloc[:,i].tolist())
print (result_temp)

它與:

result_temp = [result.iloc[:,i].tolist() for i in range(0, lenghofColumn)]

幾個其他替代品

替代1
list上下文中使用df時,您將獲得每個列名稱

[list(result[c]) for c in result]

替代2
有趣的map

list(map(list, map(result.get, result)))  

替代3
帶有tuplezip轉置

list(zip(*result.values))

隨着list

list(map(list, zip(*result.values)))

替代4

list(df.to_dict('list').values())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM