[英]Creating a Dataframe from a Dictionary
I thought that I had a very good grasp of pandas data frames, but for some reason, I can convert this dictionary to a dataframe - I'm going to post the full code with constant variables in case someone wants to tag alone:我认为我非常了解 pandas 数据帧,但由于某种原因,我可以将此字典转换为 dataframe - 我将发布带有常量变量的完整代码,以防有人想单独标记:
import pandas as pd
import numpy as np
patches = 26
min_sl = 2.1
max_sl = 104.1
array = np.arange(min_sl, max_sl + 1, step=1)
##Creating an array List
array_list = [i for i in array]
## Creating a dictionary
dict_l={i:"" for i in range(1,patches+1)}
## Populating the dictionary
start = 0
end = 4
for i in dict_l:
dict_l[i] = array_list[start:end]
start = start + 5
end = end + 5
This creates the following dictionary这将创建以下字典
{1: [2.1, 3.1, 4.1, 5.1],
2: [7.1, 8.1, 9.1, 10.1],
3: [12.1, 13.1, 14.1, 15.1],
4: [17.1, 18.1, 19.1, 20.1],
5: [22.1, 23.1, 24.1, 25.1],
6: [27.1, 28.1, 29.1, 30.1],
7: [32.1, 33.1, 34.1, 35.1],
8: [37.1, 38.1, 39.1, 40.1],
9: [42.1, 43.1, 44.1, 45.1],
10: [47.1, 48.1, 49.1, 50.1],
11: [52.1, 53.1, 54.1, 55.1],
12: [57.1, 58.1, 59.1, 60.1],
13: [62.1, 63.1, 64.1, 65.1],
14: [67.1, 68.1, 69.1, 70.1],
15: [72.1, 73.1, 74.1, 75.1],
16: [77.1, 78.1, 79.1, 80.1],
17: [82.1, 83.1, 84.1, 85.1],
18: [87.1, 88.1, 89.1, 90.1],
19: [92.1, 93.1, 94.1, 95.1],
20: [97.1, 98.1, 99.1, 100.1],
21: [102.1, 103.1, 104.1],
22: [],
23: [],
24: [],
25: [],
26: []}
What I want is a simple dataframe that puts each list in to a row, so for example index 1 will have a list with this values: [2.1, 3.1, 4.1, 5.1],我想要的是一个简单的 dataframe 将每个列表放入一行,因此例如索引 1 将有一个具有以下值的列表:[2.1, 3.1, 4.1, 5.1],
result = pd.DataFrame.from_dict(dict_l, orient='index')
result
As you can see my result is not what I want, because I want just one column如您所见,我的结果不是我想要的,因为我只想要一列
If I don't specify the function from_dict, I get an error, so I'm wondering if anyone has any idea on how to do this..如果我没有从_dict指定 function,我会收到一个错误,所以我想知道是否有人知道如何做到这一点..
I tried to concatenate all columns with a comma, which is in theory what I would like to do:我试图用逗号连接所有列,这在理论上是我想做的:
result['Line Name'] = result[[i for i in range(0,4,1)]].apply(lambda x: ', '.join(x[x.notnull()]), axis=1) result['Line Name'] = result[[i for i in range(0,4,1)]].apply(lambda x: ', '.join(x[x.notnull()]), axis=1 )
However, I get a type error TypeError: sequence item 0: expected str instance, float found但是,我收到类型错误 TypeError: sequence item 0: expected str instance, float found
Does anyone know how to accomplish this?有谁知道如何做到这一点?
df = pd.DataFrame({k:[v] for k,v in dict_l.items()}).astype(str)
df = df.stack().reset_index(drop=True)
print(df)
output: output:
0 [2.1, 3.1, 4.1, 5.1]
1 [7.1, 8.1, 9.1, 10.1]
2 [12.1, 13.1, 14.1, 15.1]
3 [17.1, 18.1, 19.1, 20.1]
4 [22.1, 23.1, 24.1, 25.1]
5 [27.1, 28.1, 29.1, 30.1]
6 [32.1, 33.1, 34.1, 35.1]
7 [37.1, 38.1, 39.1, 40.1]
8 [42.1, 43.1, 44.1, 45.1]
9 [47.1, 48.1, 49.1, 50.1]
10 [52.1, 53.1, 54.1, 55.1]
11 [57.1, 58.1, 59.1, 60.1]
12 [62.1, 63.1, 64.1, 65.1]
13 [67.1, 68.1, 69.1, 70.1]
14 [72.1, 73.1, 74.1, 75.1]
15 [77.1, 78.1, 79.1, 80.1]
16 [82.1, 83.1, 84.1, 85.1]
17 [87.1, 88.1, 89.1, 90.1]
18 [92.1, 93.1, 94.1, 95.1]
19 [97.1, 98.1, 99.1, 100.1]
20 [102.1, 103.1, 104.1]
21 []
22 []
23 []
24 []
25 []
Just put the list in the list:只需将列表放入列表中:
dict_l[i] = [array_list[start:end]]
Prints:印刷:
0
1 [2.1, 3.1, 4.1, 5.1]
2 [7.1, 8.1, 9.1, 10.1]
3 [12.1, 13.1, 14.1, 15.1]
4 [17.1, 18.1, 19.1, 20.1]
5 [22.1, 23.1, 24.1, 25.1]
6 [27.1, 28.1, 29.1, 30.1]
7 [32.1, 33.1, 34.1, 35.1]
8 [37.1, 38.1, 39.1, 40.1]
9 [42.1, 43.1, 44.1, 45.1]
10 [47.1, 48.1, 49.1, 50.1]
11 [52.1, 53.1, 54.1, 55.1]
12 [57.1, 58.1, 59.1, 60.1]
13 [62.1, 63.1, 64.1, 65.1]
14 [67.1, 68.1, 69.1, 70.1]
15 [72.1, 73.1, 74.1, 75.1]
16 [77.1, 78.1, 79.1, 80.1]
17 [82.1, 83.1, 84.1, 85.1]
18 [87.1, 88.1, 89.1, 90.1]
19 [92.1, 93.1, 94.1, 95.1]
20 [97.1, 98.1, 99.1, 100.1]
21 [102.1, 103.1, 104.1]
22 []
23 []
24 []
25 []
26 []
DataFrames are two-dimensional data structures, so the lists of the dictionary are interpreted as rows. DataFrames 是二维数据结构,因此字典的列表被解释为行。 Either construct a Series (1D) and convert it to a DataFrame
构造一个系列(1D)并将其转换为 DataFrame
result = pd.Series(dict_l).to_frame()
or wrap the dictionary values with extra brackets to create a "3rd dimension".或用额外的括号包裹字典值以创建“第三维”。
result = pd.DataFrame([[lst] for lst in dict_l.values()],
index=dict_l.keys())
Output: Output:
>>> result
0
1 [2.1, 3.1, 4.1, 5.1]
2 [7.1, 8.1, 9.1, 10.1]
3 [12.1, 13.1, 14.1, 15.1]
4 [17.1, 18.1, 19.1, 20.1]
5 [22.1, 23.1, 24.1, 25.1]
6 [27.1, 28.1, 29.1, 30.1]
7 [32.1, 33.1, 34.1, 35.1]
8 [37.1, 38.1, 39.1, 40.1]
9 [42.1, 43.1, 44.1, 45.1]
10 [47.1, 48.1, 49.1, 50.1]
11 [52.1, 53.1, 54.1, 55.1]
12 [57.1, 58.1, 59.1, 60.1]
13 [62.1, 63.1, 64.1, 65.1]
14 [67.1, 68.1, 69.1, 70.1]
15 [72.1, 73.1, 74.1, 75.1]
16 [77.1, 78.1, 79.1, 80.1]
17 [82.1, 83.1, 84.1, 85.1]
18 [87.1, 88.1, 89.1, 90.1]
19 [92.1, 93.1, 94.1, 95.1]
20 [97.1, 98.1, 99.1, 100.1]
21 [102.1, 103.1, 104.1]
22 []
23 []
24 []
25 []
26 []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.