亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將沒有元組的列表轉換為數據框

將沒有元組的列表轉換為數據框

www說 2023-07-11 16:36:48
通常,當您想要將一組數據轉換為數據框時,您需要為每一列創建一個列表,從這些列表創建一個字典,然后從該字典創建一個數據框。我想要創建的數據框有 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


查看完整回答
反對 回復 2023-07-11
  • 1 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號