1.问题描述:想要筛选Excel中的数据,筛选条件是 实际收费 > 消费金额,并且保存筛选的数据为Excel格式!
数据格式如底部所示!
2.实现:利用pandas处理数据。
3.代码如下:
import numpy as np
import pandas as pd
# a = np.arange(24).reshape(4, 6)
# a[2][4] = 100
# a[1][4] = 100
# b = a[:, 4]
# c = a[:, 5]
# # a = a[a[:, 4] > a[:, 5]]
# a = a[b > c]
# print(a)
excel_ori = pd.read_excel(io = 'data.xlsx')
a = excel_ori.values
a = a[a[:, 7] > a[:, 8]]
data_df = pd.DataFrame(a)
data_df.columns = ['单据号','商品编码','商品售价','销售数量','消费金额','消费产生的时间','收银机号','实际收费','消费金额']
# data_df.index = ['a','b','c','d','e','f','g','h']
writer = pd.ExcelWriter('ret.xlsx')
data_df.to_excel(writer, 'page_1', index=False)
writer.save()
4.源数据和处理后的数据图片:
![](https://i-blog.csdnimg.cn/blog_migrate/bc0bfe2cc0ce336ad501cb765fbe5d58.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ce80d63b669f348915527526cb7fca77.png)