共計 773 個字符,預計需要花費 2 分鐘才能閱讀完成。
使用 Python 處理 Excel 數據可以使用第三方庫 pandas
和openpyxl
。
首先,安裝這兩個庫:
pip install pandas
pip install openpyxl
然后,導入相關庫:
import pandas as pd
from openpyxl import load_workbook
讀取 Excel 數據:
df = pd.read_excel('data.xlsx')
處理 Excel 數據:
# 選擇某一列數據
col_data = df[' 列名 ']
# 選擇某一行數據
row_data = df.loc[行索引]
# 過濾數據
filtered_data = df[df[' 列名 '] > 10]
# 添加新的列
df[' 新列名 '] = df[' 列名 1 '] + df[' 列名 2 ']
# 刪除某一列
df.drop(' 列名 ', axis=1, inplace=True)
# 更新某一列數據
df.loc[行索引, ' 列名 '] = 值
# 保存數據到新的 Excel 文件
df.to_excel('new_data.xlsx', index=False) # index=False 表示不保存行索引
# 更新已有的 Excel 文件
with pd.ExcelWriter('data.xlsx', mode='a') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
注意:處理 Excel 數據時,可以使用 pandas
庫提供的各種函數和方法進行數據處理和分析,使用 openpyxl
庫進行 Excel 文件的讀寫操作。
丸趣 TV 網 – 提供最優質的資源集合!
正文完