国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(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ó)家法律
新绛县| 北宁市| 博乐市| 绥芬河市| 陇南市| 大厂| 稷山县| 黄石市| 东平县| 哈巴河县| 偏关县| 临夏市| 陆丰市| 都江堰市| 德庆县| 南平市| 兴义市| 军事| 金昌市| 博客| 莱阳市| 灌阳县| 如皋市| 佛坪县| 饶阳县| 前郭尔| 荃湾区| 南雄市| 鸡东县| 砀山县| 镇赉县| 木兰县| 临邑县| 千阳县| 宁安市| 高密市| 峨眉山市| 长沙市| 湖南省| 白河县| 富裕县|