为了账号安全,请及时绑定邮箱和手机立即绑定

通过与以前的标题名称连接来重命名 pandas 中的列标题

通过与以前的标题名称连接来重命名 pandas 中的列标题

蝴蝶刀刀 2023-10-26 16:54:28
我有一组列名称,其中名称描述多次存在。['Sl.No', 'Job', 'Description', 'Vendor', 'Description', 'WO No',       'Accounting Center ', 'Description', 'Nature Of Work', 'WO Type',       'WO Date', 'WO From Date', 'WO To Date', 'Bill No', 'Running Bill No',       'Bill Date', 'Bill Status', 'Voucher No', 'Voucher Date',       'Bank \nVoucher No', 'Bank Voucher\n Date', ' Paid Amount', 'Currency',       'WO Amt', 'Bill Amt', 'Service Tax Amt', 'VAT Amt', 'Total Tax \nAmt',       ' Advance Amt', 'Gross Amt', 'Deduction Amt', 'Net Amt']我希望名称“Description”与其后面的任何标头名称连接起来例如“Job”、“Description”将变为“Job_Description”“Vendor”、“Description”将变为“Vendor_Description”等我需要一个不引入任何硬编码的逻辑['Sl.No', 'Job', 'Job_Description', 'Vendor', 'Vendor_Description',....]
查看完整描述

3 回答

?
皈依舞

TA贡献1851条经验 获得超3个赞

你需要找出哪里Description存在using pd.Series.eq,然后使用pd.Index.to_series,这样我们才能使用pd.Series.shift

cols = df.columns.to_series()

m = cols.eq('Description') #would mark True where value is `Description`

out = cols.shift().str.strip().str.replace('\s+','_') + '_' + cols

out[~m] = cols

out.to_list()


['Sl.No', 'Job', 'Job_Description', 'Vendor', 'Vendor_Description', 'WO No',

 'Accounting Center ', 'Accounting_Center_Description', 'Nature Of Work',

 'WO Type', 'WO Date', 'WO From Date', 'WO To Date', 'Bill No', 'Running Bill No',

 'Bill Date', 'Bill Status', 'Voucher No', 'Voucher Date', 'Bank \nVoucher No',

 'Bank Voucher\n Date', ' Paid Amount', 'Currency', 'WO Amt', 'Bill Amt',

 'Service Tax Amt', 'VAT Amt', 'Total Tax \nAmt', ' Advance Amt', 'Gross Amt',

 'Deduction Amt', 'Net Amt']


查看完整回答
反对 回复 2023-10-26
?
达令说

TA贡献1821条经验 获得超6个赞

让cols成为您原始列表的名称。那么代码可能是:


D = "Description"


previous_current = zip([None] + cols[:-1], cols)

df.columns = [curr if curr != D else prev + "_" + D  for prev, curr in previous_current]

结果:


>>> df.columns

Index(['Sl.No', 'Job', 'Job_Description', 'Vendor', 'Vendor_Description',

       'WO No', 'Accounting Center ', 'Accounting Center _Description',

       'Nature Of Work', 'WO Type', 'WO Date', 'WO From Date', 'WO To Date',

       'Bill No', 'Running Bill No', 'Bill Date', 'Bill Status', 'Voucher No',

       'Voucher Date', 'Bank \nVoucher No', 'Bank Voucher\n Date',

       ' Paid Amount', 'Currency', 'WO Amt', 'Bill Amt', 'Service Tax Amt',

       'VAT Amt', 'Total Tax \nAmt', ' Advance Amt', 'Gross Amt',

       'Deduction Amt', 'Net Amt'],

      dtype='object')

说明:


我们构建了一些作为对列表的东西(previous, current):


>>> previous_current = zip([None] + cols[:-1], cols)

>>> list(previous_current)

[(None, 'Sl.No'),

 ('Sl.No', 'Job'),

 ('Job', 'Description'),

 ('Description', 'Vendor'),

 ('Vendor', 'Description'),

 ('Description', 'WO No'),

 ('WO No', 'Accounting Center '),

 ('Accounting Center ', 'Description'),

 ('Description', 'Nature Of Work'),

 ('Nature Of Work', 'WO Type'),

 ('WO Type', 'WO Date'),

 ('WO Date', 'WO From Date'),

 ('WO From Date', 'WO To Date'),

 ('WO To Date', 'Bill No'),

 ('Bill No', 'Running Bill No'),

 ('Running Bill No', 'Bill Date'),

 ('Bill Date', 'Bill Status'),

 ('Bill Status', 'Voucher No'),

 ('Voucher No', 'Voucher Date'),

 ('Voucher Date', 'Bank \nVoucher No'),

 ('Bank \nVoucher No', 'Bank Voucher\n Date'),

 ('Bank Voucher\n Date', ' Paid Amount'),

 (' Paid Amount', 'Currency'),

 ('Currency', 'WO Amt'),

 ('WO Amt', 'Bill Amt'),

 ('Bill Amt', 'Service Tax Amt'),

 ('Service Tax Amt', 'VAT Amt'),

 ('VAT Amt', 'Total Tax \nAmt'),

 ('Total Tax \nAmt', ' Advance Amt'),

 (' Advance Amt', 'Gross Amt'),

 ('Gross Amt', 'Deduction Amt'),

 ('Deduction Amt', 'Net Amt')]

然后我们迭代它,并通过该对的第二个元素(当前列标签)我们决定

  • 是否保持原样,或者

  • 将其附加前一列的标签(该对的第一个元素)


查看完整回答
反对 回复 2023-10-26
?
慕码人2483693

TA贡献1860条经验 获得超9个赞

让cols成为您原始列表的名称。那么代码可能是:


D = "Description"


previous_current = zip([None] + cols[:-1], cols)

df.columns = [curr if curr != D else prev + "_" + D  for prev, curr in previous_current]

结果:


>>> df.columns

Index(['Sl.No', 'Job', 'Job_Description', 'Vendor', 'Vendor_Description',

       'WO No', 'Accounting Center ', 'Accounting Center _Description',

       'Nature Of Work', 'WO Type', 'WO Date', 'WO From Date', 'WO To Date',

       'Bill No', 'Running Bill No', 'Bill Date', 'Bill Status', 'Voucher No',

       'Voucher Date', 'Bank \nVoucher No', 'Bank Voucher\n Date',

       ' Paid Amount', 'Currency', 'WO Amt', 'Bill Amt', 'Service Tax Amt',

       'VAT Amt', 'Total Tax \nAmt', ' Advance Amt', 'Gross Amt',

       'Deduction Amt', 'Net Amt'],

      dtype='object')

说明:


我们构建了一些作为对列表的东西(previous, current):


>>> previous_current = zip([None] + cols[:-1], cols)

>>> list(previous_current)

[(None, 'Sl.No'),

 ('Sl.No', 'Job'),

 ('Job', 'Description'),

 ('Description', 'Vendor'),

 ('Vendor', 'Description'),

 ('Description', 'WO No'),

 ('WO No', 'Accounting Center '),

 ('Accounting Center ', 'Description'),

 ('Description', 'Nature Of Work'),

 ('Nature Of Work', 'WO Type'),

 ('WO Type', 'WO Date'),

 ('WO Date', 'WO From Date'),

 ('WO From Date', 'WO To Date'),

 ('WO To Date', 'Bill No'),

 ('Bill No', 'Running Bill No'),

 ('Running Bill No', 'Bill Date'),

 ('Bill Date', 'Bill Status'),

 ('Bill Status', 'Voucher No'),

 ('Voucher No', 'Voucher Date'),

 ('Voucher Date', 'Bank \nVoucher No'),

 ('Bank \nVoucher No', 'Bank Voucher\n Date'),

 ('Bank Voucher\n Date', ' Paid Amount'),

 (' Paid Amount', 'Currency'),

 ('Currency', 'WO Amt'),

 ('WO Amt', 'Bill Amt'),

 ('Bill Amt', 'Service Tax Amt'),

 ('Service Tax Amt', 'VAT Amt'),

 ('VAT Amt', 'Total Tax \nAmt'),

 ('Total Tax \nAmt', ' Advance Amt'),

 (' Advance Amt', 'Gross Amt'),

 ('Gross Amt', 'Deduction Amt'),

 ('Deduction Amt', 'Net Amt')]


查看完整回答
反对 回复 2023-10-26
  • 3 回答
  • 0 关注
  • 145 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信