为什么我不断收到 KeyError: 'Credits' 此代码:
import pandas as pd
# Create a sample DataFrame similar to dmtm_tsr
data = {
'Reinstatement Type': ['AUTO REINSTATEMENT', 'REG REINSTATEMENT', 'REGULAR REINSTATEMENT', 'SPECIAL CON REINSTATEMENT'],
'Name': ['John Doe', 'Jane Smith', 'Alice Johnson', 'Bob Brown'],
'Email Address': ['[email protected]', '[email protected]', '[email protected]', '[email protected]'],
'Mobile': ['1234567890', '9876543210', '5555555555', '9999999999'],
'Credits': [300, 300, 300, 300],
'Message': ['Message 1', 'Message 2', 'Message 3', 'Message 4'],
'Client Reference No.': ['Ref1', 'Ref2', 'Ref3', 'Ref4'],
'Client Tags': ['Tag1', 'Tag2', 'Tag3', 'Tag4']
}
dmtm_tsr = pd.DataFrame(data)
# Your existing pivot table code
pivot_summary = dmtm_tsr.pivot_table(values=['Credits', 'Message', 'Client Reference No.', 'Client Tags'],
index=['Reinstatement Type', 'Name', 'Email Address', 'Mobile'],
aggfunc={'Credits': 'sum', 'Message': 'first', 'Client Reference No.': 'first', 'Client Tags': 'first'}).reset_index()
# Add two rows if conditions are met
condition = (pivot_summary['Reinstatement Type'].isin(['AUTO REINSTATEMENT', 'REG REINSTATEMENT', 'REGULAR REINSTATEMENT', 'SPECIAL CON REINSTATEMENT']) &
(pivot_summary['Credits'] == 300) &
(pivot_summary.groupby('Reinstatement Type')['Credits'].transform('sum') >= 350))
if condition.any():
# Add first row
row1 = {'Reinstatement Type': 'Reinstatement', 'Name': 'JOHN DOE', 'Email Address': '[email protected]',
'Mobile': '213456780', 'Credits': 5000, 'Message': 'Congratulations and thank you for supporting the Reinstatement Campaign. As a token of thanks, please enjoy these eGift credits that you can use in stores of Giftaway merchants nationwide. We look forward to your active participation throughout the campaign period!', 'Client Reference No.': 'Ref1', 'Client Tags': 'Tag1'}
# Add second row with the same information
row2 = {'Reinstatement Type': 'Reinstatement', 'Name': 'JANE SMITH', 'Email Address': '[email protected]',
'Mobile': '0987654321', 'Credits': 5000, 'Message': 'Congratulations and thank you for supporting the Reinstatement Campaign. As a token of thanks, please enjoy these eGift credits that you can use in stores of Giftaway merchants nationwide. We look forward to your active participation throughout the campaign period!', 'Client Reference No.': 'REF2', 'Client Tags': 'TAG2'}
# Append the new rows to the pivot table
pivot_summary = pivot_summary.append([row1, row2], ignore_index=True)
# Adjust the column order
column_order = ['Reinstatement Type', 'Name', 'Email Address', 'Mobile', 'Credits', 'Message', 'Client Reference No.', 'Client Tags']
pivot_summary = pivot_summary[column_order]
# Save the DataFrame to a CSV file with tab-separated values
csv_file_path = "DMTM_Template.csv"
pivot_summary.to_csv(csv_file_path, sep='\t', index=False)
# Print a success message
print(f"DataFrame saved to {csv_file_path}")
好的,这就是我解决这个问题的整个代码块。每当我尝试显示
print(pivot_summary.head())
时,它只显示这些列:['恢复类型'、'姓名'、'电子邮件地址'、'手机']
这是该错误的回溯: `关键错误:'学分'
上述异常是导致以下异常的直接原因:
KeyError Traceback(最近一次调用最后一次) [251] 中的单元格,第 8 行
----> 8 (pivot_summary['学分'] == 300) & ... 第3801章否则我们会失败并重新加注 第3802章 第3803章
密钥错误:“制作人员”`
这是旋转后桌子的状态:
print(pivot_summary)
Reinstatement Type Name Email Address Mobile Client Reference No. Client Tags Credits Message
0 AUTO REINSTATEMENT John Doe [email protected] 1234567890 Ref1 Tag1 300 Message 1
1 REG REINSTATEMENT Jane Smith [email protected] 9876543210 Ref2 Tag2 300 Message 2
2 REGULAR REINSTATEMENT Alice Johnson [email protected] 5555555555 Ref3 Tag3 300 Message 3
3 SPECIAL CON REINSTATEMENT Bob Brown [email protected] 9999999999 Ref4 Tag4 300 Message 4
它(当然)有一个 Credits
列,其余代码将能够正常运行而不会出现错误。