简体   繁体   中英

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

I'm a newbie in python and pandas. I'm trying to put all rows with one column into each index of the list.

Here is my code.

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

the ouput of print is like

           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

I want to put whole rows with each column to the list. For example, there is a list result_temp = []

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

However, it gives me an error that says Error: TypeError: unhashable type

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

Is there any ways to do it...?

Please help me out here...

Thanks in advance.

The best is transpose values, convert to numpy array and last to list s:

result_temp = result.T.values.tolist()

I think you can use list comprehension where loop by columns names and select each column by name by [] and convert to list :

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

it is same as:

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

If want use your code need DataFrame.iloc for select column by position:

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

It is same as:

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

Couple of other alternatives

Alt 1
When using df in a list context, you'll get each column name

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

Alt 2
Fun with map

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

Alt 3
zip transpose with tuple s

list(zip(*result.values))

With list s

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

Alt 4

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM