python pandas计算excel中每行数据与最后一行数据的比值并筛选出比值大于0.6的数据,然后确定这个比值来源于哪行哪列数据
时间: 2024-03-22 08:41:07 浏览: 45
可以按照以下步骤实现:
1. 读取Excel文件为DataFrame对象,假设文件名为data.xlsx:
```python
import pandas as pd
df = pd.read_excel('data.xlsx')
```
2. 计算每行数据与最后一行数据的比值:
```python
last_row = df.iloc[-1] # 获取最后一行数据
ratio = df.apply(lambda x: x / last_row, axis=1) # 计算比值
```
3. 筛选比值大于0.6的数据:
```python
ratio_gt_06 = ratio[ratio > 0.6].dropna(how='all')
```
4. 确定比值来源于哪行哪列数据:
```python
for idx, row in ratio_gt_06.iterrows():
for col, val in row.iteritems():
print(f"比值 {val} 来源于第 {idx} 行,第 {col} 列")
```
完整代码如下:
```python
import pandas as pd
df = pd.read_excel('data.xlsx')
last_row = df.iloc[-1]
ratio = df.apply(lambda x: x / last_row, axis=1)
ratio_gt_06 = ratio[ratio > 0.6].dropna(how='all')
for idx, row in ratio_gt_06.iterrows():
for col, val in row.iteritems():
print(f"比值 {val} 来源于第 {idx} 行,第 {col} 列")
```
阅读全文