Pandas KeyError: «Кредиты»Python

Программы на Python
Anonymous
Pandas KeyError: «Кредиты»

Сообщение Anonymous »

Почему я продолжаю получать сообщение об ошибке KeyError: «Кредиты» для этого кода:

Код: Выделить всё

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': ['john@example.com', 'jane@example.com', 'alice@example.com', 'bob@example.com'],
'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': 'john@example.com',
'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': 'JANESMITH@EXAMPLE.COM',
'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()), отображаются только следующие столбцы: ['Тип восстановления', 'Имя', 'Адрес электронной почты', 'Мобильный']

Подробнее здесь: https://stackoverflow.com/questions/787 ... or-credits

Вернуться в «Python»