简体   繁体   English

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

[英]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 = [] 例如,有一个列表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 但是,它给出了一个错误: 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: 最好的是转置值,转换为numpy array ,最后是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 : 我认为您可以使用list comprehension ,其中按列名称循环,并按[]按名称选择每列,并转换为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: 如果想要使用你的代码需要DataFrame.iloc来按位置选择列:

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 替代1
When using df in a list context, you'll get each column name list上下文中使用df时,您将获得每个列名称

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

Alt 2 替代2
Fun with map 有趣的map

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

Alt 3 替代3
zip transpose with tuple s 带有tuplezip转置

list(zip(*result.values))

With list s 随着list

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

Alt 4 替代4

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何按子列表的内容对子列表列表进行排序,其中子列表包含字符串和布尔值? - How to sort a list of sub-lists by the contents of sub-lists, where sub-lists contain strings and booleans? python-从带有子列表的列表创建值字典 - python - create dictionary of values from list with sub-lists 列表的列表,减去每个子列表中的值并将结果存储在新的子列表中 - A list of lists, substract the values in each sub-list and store the results in new sub-lists Python - 创建包含多个子列表的列表 - Python - Create list with multiple sub-lists 列表中子列表上的操作 - Operations on sub-lists in list 将列表的子列表汇总到一个新列表中 - Summing sub-lists of lists into a new list 如何创建具有 3 个元素的子列表,但是如果子列表只有一个元素,则改为创建两个具有 2 个元素的子列表? - How to create sub-lists with 3 elements, but if a sub-list gets a single element then instead create two with 2 elements? 从列表中创建唯一项目的子列表 - Create sub-lists of the unique items from a list 创建元组列表的列表,其中每个元组是第一个出现的字母及其列表中的行和列 - Create a list of lists of tuples where each tuple is the first occurrence of a letter along with its row and column in the list of lists 如何根据时间戳值计算子列表的平均值 - How to calculate average of sub-lists based on Timestamp values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM