5

I have the following sample table:

df = pd.DataFrame({'Name': ['Bob', 'Bob', 'Susan', 'Susan', 'Jane','Jane'],
                   'Status': ['Paid', 'Paid', 'Paid OTP', 'Overdue', '', 'Upsell'],
                   'Amt': [100, 200, 300, 200, 0,60]})

I am trying to create a pivot table (which works) with the following code:

table = pd.pivot_table(df, values='Amt', index=['Name'],columns=['Status'], aggfunc=np.sum, fill_value=0)

however I need to include a persistent column, for example: "To Be Paid" irrespective if there is a value or not in the table. The reason for this is to show a 0 value if "To Be Paid" is filled in for that specific month.

python: 3.6.8

pandas: 0.25.0

2 Answers 2

5

Use Index.union for add new value to columns names if not exist with DataFrame.reindex:

cols = table.columns.union(['To Be Paid'], sort=False)
table = table.reindex(cols, axis=1, fill_value=0)
print (table)
          Overdue  Paid  Paid OTP  Upsell  To Be Paid
Name                                                 
Bob    0        0   300         0       0           0
Jane   0        0     0         0      60           0
Susan  0      200     0       300       0           0
Sign up to request clarification or add additional context in comments.

Comments

2

You can also convert your Status column to a categorical and ensure that "To Be Paid" is a present category (even if it is not represented in the data)

df = pd.DataFrame({'Name': ['Bob', 'Bob', 'Susan', 'Susan', 'Jane','Jane'],
                   'Status': ['Paid', 'Paid', 'Paid OTP', 'Overdue', '', 'Upsell'],
                   'Amt': [100, 200, 300, 200, 0,60]})

df["Status"] = pd.Categorical(df["Status"])
if "To Be Paid" not in df["Status"].cat.categories:
    df["Status"].cat.add_categories("To Be Paid", inplace=True)


df.pivot_table(values='Amt', index='Name', columns='Status', aggfunc=np.sum, fill_value=0, dropna=False)
Status     Overdue  Paid  Paid OTP  Upsell  To Be Paid
Name                                                  
Bob     0  0        300   0         0       0         
Jane    0  0        0     0         60      0         
Susan   0  200      0     300       0       0 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.