Python pandas 處理大量資料--提速方法
請教一下, 我有個EXCEL FILE 有3個COLUMN: A/B /C, 如果A欄的資料包含C 欄的資料, B 欄內就放入C 欄資料(對應到A欄位置)。
A 欄資料有3萬筆, C 欄資料有12 萬筆, 用下面段CODE 行左10幾分鐘都未行完, 請問有冇提速方法?- import pandas as pd
- df = pd.read_excel('your_file.xlsx')
- df['A'] = df['A'].astype(str)
- df['B'] = df['B'].astype(str)
- df['C'] = df['C'].astype(str)
- def update_B(row):
- for idx, a_value in df['A'].items():
- if row['C'] in a_value:
- df.at[idx, 'B'] = row['C']
- return row
- df.apply(update_B, axis=1)
- df.to_excel('output_file.xlsx', index=False)
複製代碼 |
|