五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

pandas如何用幾秒鐘處理擁有大量行的excel表

2023-06-22 22:48 作者:一個(gè)無昵稱的人  | 我要投稿

如果用pandas對(duì)一個(gè)擁有十分可觀的行的excel表進(jìn)行數(shù)據(jù)處理,此時(shí)用dataframe進(jìn)行數(shù)據(jù)比對(duì)是十分不明智的選擇,因?yàn)閐ataframe其本身為對(duì)象,會(huì)占用十分可觀的內(nèi)存空間。如果執(zhí)行以下典型反例會(huì)導(dǎo)致內(nèi)存越吃越多,最后電腦必然會(huì)徹底卡死崩潰。


import pandas as pd


excel_file = pd.ExcelFile('【總】input.xlsx')

df1 = pd.read_excel(excel_file, sheet_name='Sheet1')

df2 = pd.read_excel(excel_file, sheet_name='Sheet2')


i = 0

j = 0

while i < len(df1):

? ? print('第'+str(i)+f'行,進(jìn)度{round(i/24956*100,1)}%')

? ? while j < len(df2):

? ? ? ? if df1.iloc[i, 0] == df2.iloc[j, 0] and df1.iloc[i, 1] == df2.iloc[j, 1] and df1.iloc[i, 5] == df2.iloc[j, 3]:

? ? ? ? ? ? df1.iloc[i, 14] = df2.iloc[j, 16]

? ? ? ? ? ? df1.iloc[i, 15] = df2.iloc[j, 17]

? ? ? ? ? ? df1.iloc[i, 16] = df2.iloc[j, 18]

? ? ? ? ? ? df1.iloc[i, 17] = df2.iloc[j, 19]

? ? ? ? ? ? df1.iloc[i, 18] = df2.iloc[j, 20]

? ? ? ? ? ? df1.iloc[i, 19] = df2.iloc[j, 21]

? ? ? ? j += 1

? ? j = 0

? ? i += 1

df1.to_excel('測(cè)試.xlsx', index=False)


那么該如何優(yōu)化上述代碼?不妨將dataframe對(duì)象徹底轉(zhuǎn)換成列表,然后進(jìn)行列表與列表之間的數(shù)據(jù)比對(duì)。唯一的缺點(diǎn)是會(huì)出來一堆沒有任何用處的警告。以下為優(yōu)化方案。


import pandas as pd


excel_file = pd.ExcelFile('【總】input.xlsx')

df1 = pd.read_excel(excel_file, sheet_name='Sheet1')

df2 = pd.read_excel(excel_file, sheet_name='Sheet2')


# 將df1和df2轉(zhuǎn)換為列表

list1 = df1.values.tolist()

list2 = df2.values.tolist()


# 進(jìn)行數(shù)據(jù)對(duì)比并更新列表

i = 0

j = 0

while i < len(list1):

? ? print('第' + str(i) + f'行,進(jìn)度{round(i / 24956 * 100, 1)}%')

? ? while j < len(list2):

? ? ? ? if str(list1[i][0]) == str(list2[j][0]) and str(list1[i][1]) == str(list2[j][1]) and str(list1[i][5]) == str(list2[j][3]):

? ? ? ? ? ? list1[i][14] = list2[j][16]

? ? ? ? ? ? list1[i][15] = list2[j][17]

? ? ? ? ? ? list1[i][16] = list2[j][18]

? ? ? ? ? ? list1[i][17] = list2[j][19]

? ? ? ? ? ? list1[i][18] = list2[j][20]

? ? ? ? ? ? list1[i][19] = list2[j][21]

? ? ? ? j += 1

? ? j = 0

? ? i += 1


# 將更新后的列表轉(zhuǎn)換回DataFrame

df1_updated = pd.DataFrame(list1, columns=df1.columns)

df1_updated.to_excel('測(cè)試.xlsx', index=False)

pandas如何用幾秒鐘處理擁有大量行的excel表的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
建水县| 漳州市| 五莲县| 胶南市| 永德县| 丰都县| 始兴县| 屏南县| 衡南县| 通山县| 夏津县| 吉水县| 德州市| 新巴尔虎右旗| 正宁县| 工布江达县| 崇礼县| 波密县| 唐河县| 保德县| 丁青县| 广德县| 岳西县| 阜宁县| 固阳县| 宜宾县| 温州市| 鄂托克旗| 永昌县| 福鼎市| 浦东新区| 漳平市| 汾阳市| 商水县| 共和县| 洮南市| 桐柏县| 广丰县| 行唐县| 苍山县| 勃利县|