首页 文章

Python根据第一列中的键组合了两列

提问于
浏览
3

假设我在excel文件中有两列,如下所示:

1 1
1 2
2 3
3 4
4 5
5 6
1 3

我的目标是实现两列之间的映射 . 如果第一列中的值在多行中相同,请在第二列中添加相应的值 . 所以我的输出应该是这样的:[1:6,2:3,3:4,4:5,5:6]

逻辑:数字“1”存在于3行中,其对应值为1,2和3.因此,键1的总值变为1 2 3 = 6 .

我从一个方法开始,并尽可能地:

import xlrd
book = xlrd.open_workbook('C:\\Users\\a593977\\Desktop\\ExcelTest.xlsx')
sheet = book.sheet_by_name('Sheet1')
data = [[sheet.cell_value(c, r) for c in range(sheet.nrows)] for r in range(sheet.ncols)]
firstColumn=data[0]
firstColumn=sorted(firstColumn)
secondColumn=data[1]
secondColumn=sorted(secondColumn)
print(list(zip(firstColumn,secondColumn)))

此代码的输出是:

[(1.0,1.0),(1.0,2.0),(1.0,3.0),(2.0,3.0),(3.0,4.0),(4.0,5.0),(5.0,6.0)]

但目标是:[1:6,2:3,3:4,4:5,5:6] . 我该如何进一步处理?

3 回答

  • 3

    使用熊猫 . 试试 groupbysumagg .

    import pandas as pd
    
    df = pd.read_excel('C:\\Users\\a593977\\Desktop\\ExcelTest.xlsx', header=None)
    res = (df
          .groupby(df.columns[0], as_index=False, sort=False)[df.columns[1]]
          .sum()
          .astype(str)
          .agg(':'.join, 1)
          .tolist()
    )
    
    print(res)
    ['1:6', '2:3', '3:4', '4:5', '5:6']
    
  • 0

    没有熊猫,从 [(1.0, 1.0), (1.0, 2.0), (1.0, 3.0), (2.0, 3.0), (3.0, 4.0), (4.0, 5.0), (5.0, 6.0)]{1: 6, 2: 3, 3: 4, 4: 5, 5: 6} .

    只需 Counter (这是一个专门的 defaultdict ):

    from collections import Counter
    
    x = [(1.0, 1.0), (1.0, 2.0), (1.0, 3.0), (2.0, 3.0), (3.0, 4.0), (4.0, 5.0), (5.0, 6.0)]
    
    sums = Counter()
    for key, value in x:
        sums[key] += value
    
    print(sums)
    

    输出是

    Counter({1.0: 6.0, 5.0: 6.0, 4.0: 5.0, 3.0: 4.0, 2.0: 3.0})
    

    如果你需要的不是总和,你可以使用 defaultdict .

  • 0

    您可以使用Pandas读取数据,然后使用f-strings进行列表理解(Python 3.6中提供) .

    df = pd.read_excel('file.xlsx', header=None)
    
    df_sum = df.groupby(0, as_index=False)[1].sum()
    
    res = [f'{i}:{j}' for i, j in df_sum.itertuples(index=False)]
    
    ['1:6', '2:3', '3:4', '4:5', '5:6']
    

相关问题