通过设置固定长度将列表转换为字典

时间:2019-01-10 19:59:03

标签: python pandas list dictionary dataframe

我有一个列表webClient.get().uri("full url here", MyDTO.class) ,我想将其转换为具有固定列数的pandas数据框。

结果应为:

data=['CDs', 1, 'J12345','Rainbow', None, 'Styles', 2, 'J12345', 'Rainbow', None, 'Folk', 3, 'J12345', 'Rainbow', None]

3 个答案:

答案 0 :(得分:4)

设置

num_cols = 5
cols = ['category', 'num', 'series', 'title', 'brand']

numpy.reshape

d = np.reshape(data, (-1, num_cols))  
pd.DataFrame(d, columns=cols)

  category num  series    title brand
0      CDs   1  J12345  Rainbow  None
1   Styles   2  J12345  Rainbow  None
2     Folk   3  J12345  Rainbow  None

答案 1 :(得分:1)

您可以这样做:

import pandas as pd

data = ['CDs', 1, 'J12345','Rainbow', None,
        'Styles', 2, 'J12345', 'Rainbow', None,
        'Folk', 3, 'J12345', 'Rainbow', None]

df = pd.DataFrame([data[i:i+5] for i in range(0, len(data), 5)], columns=['category', 'num', 'series',  'title', 'brand'])
print(df)

输出

  category  num  series    title brand
0      CDs    1  J12345  Rainbow  None
1   Styles    2  J12345  Rainbow  None
2     Folk    3  J12345  Rainbow  None

声明:

[data[i:i+5] for i in range(0, len(data), 5)]

创建以下列表列表:

[['CDs', 1, 'J12345', 'Rainbow', None], ['Styles', 2, 'J12345', 'Rainbow', None], ['Folk', 3, 'J12345', 'Rainbow', None]]

以上内容可以直接传递给熊猫。

答案 2 :(得分:1)

与@ user3483203几乎相同的想法,

a = np.array(data)
nrows = 3
ncols = a.shape[0]//nrows
df = pd.DataFrame(a.reshape(nrows, ncols), columns=['category', 'num', 'series', 'title', 'brand'] )

    category    num series  title   brand
0   CDs         1   J12345  Rainbow None
1   Styles      2   J12345  Rainbow None
2   Folk        3   J12345  Rainbow None