将不包含元组的列表转换为数据帧



通常,当您想要创建一个将一组数据转换为数据帧时,您可以为每列创建一个列表,从这些列表创建一个字典,然后从字典创建一个数据帧。

我要创建的数据帧有75列,所有列的行数都相同。一个接一个地定义列表是行不通的。相反,我决定制作一个单独的列表,并迭代地将每行的某个块放在数据帧上。在这里,我将举一个例子,将列表转换为数据帧:

lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# Example list
df = 
a b c d e
0  0 2 4 6 8
1  1 3 5 7 9
# Result I want from the example list

这是我的测试代码:

import pandas as pd
import numpy as np
dict = {'a':[], 'b':[], 'c':[], 'd':[], 'e':[]}
df = pd.DataFrame(dict)
# Here is my test data frame, it contains 5 columns and no rows.
lst = np.arange(10).tolist()
# This is my test list, it looks like this lst = [0, 2, …, 9]
for i in range(len(lst)):
df.iloc[:, i] = df.iloc[:, i]
.append(pd.Series(lst[2 * i:2 * i + 2]))
# This code is supposed to put two entries per column for the whole data frame.
# For the first column, i = 0, so [2 * (0):2 * (0) + 2] = [0:2]
# df.iloc[:, 0] = lst[0:2], so df.iloc[:, 0] = [0, 1]
# Second column i = 1, so [2 * (1):2 * (1) + 2] = [2:4]
# df.iloc[:, 1] = lst[2:4], so df.iloc[:, 1] = [2, 3]
# This is how the code was supposed to allocate lst to df.
# However it outputs an error.

当我运行这个代码时,我得到了这个错误:

ValueError: cannot reindex from a duplicate axis

当我添加ignore_index = True以使我具有时

for i in range(len(lst)):
df.iloc[:, i] = df.iloc[:, i]
.append(pd.Series(lst[2 * i:2 * i + 2]), ignore_index = True)

我得到这个错误:

IndexError: single positional indexer is out-of-bounds

运行完代码后,我检查了df的结果。无论我是否忽略索引,输出都是一样的。

In: df
Out:
a   b   c   d   e
0  0 NaN NaN NaN NaN
1  1 NaN NaN NaN NaN

第一个循环似乎运行良好,但在尝试填充第二列时出错。

有人知道如何让它发挥作用吗?非常感谢。

IIUC:

lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
alst = np.array(lst)
df = pd.DataFrame(alst.reshape(2,-1, order='F'), columns = [*'abcde'])
print(df)

输出:

a  b  c  d  e
0  0  2  4  6  8
1  1  3  5  7  9

最新更新