如何使用熊猫使用python剪切和粘贴列

时间:2018-11-19 21:27:43

标签: python pandas

df = pd.read_excel('C:/Python/Python37/Files/Original.xls')

#Delete columns
df.drop(['StartDate', 'EndDate','EmployeeID'], axis = 1, inplace=True)

#Renames Columns
df.rename(columns={'CheckNumber': 'W/E Date', 'CheckBranch': 
'Branch','DeductionAmount':'Amount'},inplace=True)


[['Branch','Deduction','CheckDate','W/E Date','SSN','LastName','FirstName','Amount','Agency','CaseNumber']]

writer = pd.ExcelWriter('C:/Python/Python37/Files/Deductions Report.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()

我有分支的地方,扣除......这就是我希望列出各列的方式。每列都有我需要随每列保留的特定信息。

2 个答案:

答案 0 :(得分:0)

你很近。尝试用以下内容替换该行:

df = df[['Branch','Deduction','CheckDate','W/E Date',
         'SSN','LastName','FirstName','Amount','Agency','CaseNumber']]

答案 1 :(得分:0)

尝试一下

df = pd.read_excel('C:/Python/Python37/Files/Original.xls')

#Delete columns
df.drop(['StartDate', 'EndDate','EmployeeID'], axis = 1, inplace=True)

#Renames Columns
df.rename(columns={'CheckNumber': 'W/E Date', 'CheckBranch': 
'Branch','DeductionAmount':'Amount'},inplace=True)


df = df[['Branch','Deduction','CheckDate','W/E Date','SSN','LastName','FirstName','Amount','Agency','CaseNumber']]

writer = pd.ExcelWriter('C:/Python/Python37/Files/Deductions Report.xlsx')
df.to_excel(writer,'Sheet1')
writer.save()