【问题标题】:Get count of data from particular Excel cell using python使用 python 从特定 Excel 单元格中获取数据计数
【发布时间】:2021-03-03 01:20:12
【问题描述】:

我正在使用 pandas 读取如下 excel 文件并将结果写入数据框。

我想获取每个 Testcase 的“预期结果”列中存在的行数。我使用了 len 函数,它抛出 "TypeError: object of type 'numpy.int64' has no len()" 错误。有没有办法从 excel 中为 python 中的每个测试捕获行数。

这是我的代码

        df = pd.read_excel("input_test_2.xlsx")
        testcases = df['Test'].values
        expected_result = df['Expected Result'].values
        
        for i in range(0,len(df)):
                testcase_nm = testcases[i]
                _expected = expected_result[i]
                print("Count of Expected Result:" , len(_expected))

This is the Output I am looking for :
Testcase-1 , Count of Expected Result: 1
Testcase-2 , Count of Expected Result: 3

【问题讨论】:

    标签: python-3.x pandas dataframe


    【解决方案1】:

    在没有看到数据框数据的情况下,很难说这是否可行,因为尚不清楚 pandas 如何处理合并的 excel 数据。

    一般来说:

    df_counts =  df.groupby('Test').count().reset_index() # won't give you the text, but a new dataframe 
    

    【讨论】:

      猜你喜欢
      • 2018-04-20
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多