将没有元组的列表转换为数据框

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


我想要创建的数据框有 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

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


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


www说
浏览 134回答 1
1回答

暮色呼如

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  e0  0  2  4  6  81  1  3  5  7  9
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python