python如何从指定行开始,计算3个不同excel文件第二列相同位置的小数值并算三个数的均值写入新的excel文件
时间: 2024-01-28 13:05:08 浏览: 74
可以使用Python的pandas和openpyxl库来实现此功能。以下是实现的步骤:
1. 导入需要的库:
```python
import pandas as pd
from openpyxl import Workbook, load_workbook
```
2. 加载三个excel文件:
```python
file1 = pd.read_excel('file1.xlsx')
file2 = pd.read_excel('file2.xlsx')
file3 = pd.read_excel('file3.xlsx')
```
3. 选择指定行开始的数据:
```python
start_row = 5
file1_data = file1.iloc[start_row:, 1]
file2_data = file2.iloc[start_row:, 1]
file3_data = file3.iloc[start_row:, 1]
```
4. 计算三个数的均值:
```python
mean_data = (file1_data + file2_data + file3_data) / 3
```
5. 创建一个新的excel文件并写入数据:
```python
wb = Workbook()
ws = wb.active
ws.title = 'mean_data'
for i, val in enumerate(mean_data):
ws.cell(row=i+1, column=1, value=val)
wb.save('mean_data.xlsx')
```
完整代码如下:
```python
import pandas as pd
from openpyxl import Workbook, load_workbook
file1 = pd.read_excel('file1.xlsx')
file2 = pd.read_excel('file2.xlsx')
file3 = pd.read_excel('file3.xlsx')
start_row = 5
file1_data = file1.iloc[start_row:, 1]
file2_data = file2.iloc[start_row:, 1]
file3_data = file3.iloc[start_row:, 1]
mean_data = (file1_data + file2_data + file3_data) / 3
wb = Workbook()
ws = wb.active
ws.title = 'mean_data'
for i, val in enumerate(mean_data):
ws.cell(row=i+1, column=1, value=val)
wb.save('mean_data.xlsx')
```
注意:需要安装pandas和openpyxl库。可以使用以下命令安装:
```python
pip install pandas openpyxl
```
阅读全文