【问题标题】:is there a possible way to merge excel rows for duplicete cells in a column with python?有没有一种可能的方法可以用 python 合并列中重复单元格的 excel 行?
【发布时间】:2020-05-25 23:37:40
【问题描述】:

我还是 python 新手,你能帮我解决这个问题吗
i have this excel sheet and i want it to be like this

【问题讨论】:

标签: python excel


【解决方案1】:

您可以像这样将 csv 数据转换为 panda 数据框:

import pandas as pd 

df = pd.read_csv("Input.csv") 

然后像这样进行数据操作:

df = df.groupby(['Name'])['Training'].apply(', '.join).reset_index()

最后,创建一个输出 csv 文件:

df.to_csv('Output.csv', sep='\t')

【讨论】:

    【解决方案2】:

    您可以使用pandas 创建DataFrame 来操作Excel 工作表信息。首先,使用函数read_excel 加载文件(这将创建一个DataFrame),然后使用函数groupbyapply 连接字符串。

    import pandas as pd
    
    # Read the Excel File
    df = pd.read_excel('tmp.xlsx')
    
    # Group by the column(s) that you need.
    # Finally, use the apply function to arrange the data
    df.groupby(['Name'])['Training'].apply(','.join).reset_index( )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2011-12-22
      • 1970-01-01
      • 2017-11-14
      • 1970-01-01
      • 2012-01-05
      相关资源
      最近更新 更多