使用python修改Excel文件

2024-10-02 12:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个excel文件,第一列中有公共项,如何使用python删除第一列中的公共项而不影响文件的其余部分

我的档案

 Column1   Column2             Column2
 PinkFloyd Wish You Were Here  Wish You Were Here
 PinkFloyd Comfortably Numb    The Wall
 AC_DC     Highway to Hell     Highway to Hell
 AC_DC     Thunderstruck       The Razors Edge

所需输出-

 Column1   Column2             Column3
 PinkFloyd Wish You Were Here  Wish You Were Here
           Comfortably Numb    The Wall
 AC_DC     Highway to Hell     Highway to Hell
           Thunderstruck       The Razors Edge

Tags: 文件thetoyouheredcaccolumn1
1条回答
网友
1楼 · 发布于 2024-10-02 12:38:30

使用pandas,特别是pandas.DataFrame.drop_duplicates

import pandas as pd

df = pd.read_excel('my_xls.xls')

# Find and drop duplicates in Column1
df['Column1'] = df.Column1.drop_duplicates()

# Open pandas ExcelWriter and write to *.xls file
with pd.ExcelWriter('my_xls.xls') as writer:
    df.to_excel(writer, index=False)

相关问题 更多 >

    热门问题