通常,當您想要將一組數據轉換為數據框時,您需要為每一列創建一個列表,從這些列表創建一個字典,然后從該字典創建一個數據框。我想要創建的數據框有 75 列,全部具有相同的行數。逐一定義列表是行不通的。相反,我決定創建一個列表,并迭代地將每行的某個塊放入數據幀中。這里我將舉一個例子,將列表轉換為數據框:lst = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]# Example listdf = a b c d e0 0 2 4 6 81 1 3 5 7 9# Result I want from the example list這是我的測試代碼:import pandas as pdimport numpy as npdict = {'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: dfOut: a b c d e0 0 NaN NaN NaN NaN1 1 NaN NaN NaN NaN第一個循環似乎運行良好,但在嘗試填充第二列時發生錯誤。有人知道如何讓它發揮作用嗎?謝謝。
1 回答

暮色呼如
TA貢獻1853條經驗 獲得超9個贊
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
添加回答
舉報
0/150
提交
取消