这是我的Excel工作表中的一个示例文档(我无法上传Excel工作表的屏幕截图,所以我尝试制作一个具有4个属性/列的类似表格表单)。我想用Python编写代码,这样我就可以计算第1列中的任何电影名称对于第4列中的特定值出现的次数。你知道吗
例如,死亡诗人协会(Dead Poety society)为A、D和E提供服务。最长的旅行为C提供服务,为D提供两次服务
+====================+====================+============+=========+
| Column1 | Column2 | Column3 | Column4 |
+====================+====================+============+=========+
| Dead poet society | Julia Roberts | London | A |
+--------------------+--------------------+------------+---------+
| Before sunrise | Sandra Bullock | Paris | A |
+--------------------+--------------------+------------+---------+
| Finding Dory | Emma Stone | Rome | A |
+--------------------+--------------------+------------+---------+
| Blood diamond | Anne Hathaway | Canada | A |
+--------------------+--------------------+------------+---------+
| A Beautiful mind | Amanda Seyfried | Scotland | B |
+--------------------+--------------------+------------+---------+
| Blood diamond | Anne Hathaway | Canada | B |
+--------------------+--------------------+------------+---------+
| Before sunrise | Sandra Bullock | Paris | B |
+--------------------+--------------------+------------+---------+
| The longest ride | Reese Witherspoon | Denmark | C |
+--------------------+--------------------+------------+---------+
| Marley and me | Jennifer Aniston | Germany | C |
+--------------------+--------------------+------------+---------+
| The longest ride | Reese Witherspoon | Denmark | D |
+--------------------+--------------------+------------+---------+
| Dead poet society | Julia Roberts | London | D |
+--------------------+--------------------+------------+---------+
| Remember me | Natalie Portman | Bulgaria | D |
+--------------------+--------------------+------------+---------+
| Inception | Kate Winslet | Sweden | D |
+--------------------+--------------------+------------+---------+
| The longest ride | Reese Witherspoon | Denmark | D |
+--------------------+--------------------+------------+---------+
| Gone with the wind | Scarlett Johansson | Brazil | E |
+--------------------+--------------------+------------+---------+
| Dead poet society | Julia Roberts | London | E |
+--------------------+--------------------+------------+---------+
| Before sunrise | Sandra Bullock | Paris | E |
+--------------------+--------------------+------------+---------+
| Midnight in Paris | Meg Ryan | Queensland | E |
+--------------------+--------------------+------------+---------+
这是我用过的代码,但没有用。你知道吗
import xlrd
import pandas as pd
wb = xlrd.open_workbook('sample_docu.xlsx')
cells = s.cell_value(rowx=0, colx=0)
cells_2 = s.cell_value(rowx=2, colx=3)
count=0
if cells in cells_2:
count=count+1
print('Count={}'.format(count))
注意到您的代码尝试,imported pandas,我将展示如何使用pandas实现这一点,因为它使这一点非常简单。你知道吗
代码:
测试代码:
结果:
相关问题 更多 >
编程相关推荐