第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

將沒有元組的列表轉(zhuǎn)換為數(shù)據(jù)框

將沒有元組的列表轉(zhuǎn)換為數(shù)據(jù)框

www說 2023-07-11 16:36:48
通常,當(dāng)您想要將一組數(shù)據(jù)轉(zhuǎn)換為數(shù)據(jù)框時,您需要為每一列創(chuàng)建一個列表,從這些列表創(chuàng)建一個字典,然后從該字典創(chuàng)建一個數(shù)據(jù)框。我想要創(chuàng)建的數(shù)據(jù)框有 75 列,全部具有相同的行數(shù)。逐一定義列表是行不通的。相反,我決定創(chuàng)建一個列表,并迭代地將每行的某個塊放入數(shù)據(jù)幀中。這里我將舉一個例子,將列表轉(zhuǎn)換為數(shù)據(jù)框: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.當(dāng)我運(yùn)行此代碼時,我收到此錯誤:ValueError: cannot reindex from a duplicate axis當(dāng)我添加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運(yùn)行代碼后,我檢查了結(jié)果df。無論我是否忽略索引,輸出都是相同的。In: dfOut:   a   b   c   d   e0  0 NaN NaN NaN NaN1  1 NaN NaN NaN NaN第一個循環(huán)似乎運(yùn)行良好,但在嘗試填充第二列時發(fā)生錯誤。有人知道如何讓它發(fā)揮作用嗎?謝謝。
查看完整描述

1 回答

?
暮色呼如

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超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


查看完整回答
反對 回復(fù) 2023-07-11
  • 1 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號