1

I am looking for some help on a pandas data frame.

I have a data frame with the following structure

Date(indexed)      Total Clients   Sales Headcount    Total Products
2019-11-01         1005            5                  4
2019-12-01         1033            5                  5
2020-01-01         1045            10                 6
2020-02-01         1124            10                 10
2020-03-01         1199            10                 11

How can I fill in the column total products with 0's if the date is after 2020-01-01?

Expected outcome:

Date(indexed)      Total Clients   Sales Headcount    Total Products
2019-11-01         1005            5                  4
2019-12-01         1033            5                  5
2020-01-01         1045            10                 6
2020-02-01         1124            10                 0
2020-03-01         1199            10                 0
2
  • 2
    using .loc and assign it back Commented Feb 10, 2020 at 22:05
  • Have you tried anything, done any research? Commented Feb 11, 2020 at 1:40

2 Answers 2

2

Make sure that your date column contains timestamps.

# Assuming `Date(indexed)` means that this column is the index of the dataframe.
df.index = pd.to_datetime(df.index)

Then use .loc to set all values from and including 2020 to zero.

df.loc['2020':, 'Total Products'] = 0

>>> df
            Total Clients  Sales Headcount  Total Products
Date                                                      
2019-11-01           1005                5               4
2019-12-01           1033                5               5
2020-01-01           1045               10               0
2020-02-01           1124               10               0
2020-03-01           1199               10               0
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I did mean that is an indexed column. If I wanted to use your logic for everything larger than "2020-01-01" set that column equal to 0. How do I do that?
df.loc['2020':, :] = 0
1

using .loc to assign values based on a boolean.

# df['Date(indexed)'] = pd.to_datetime(df['Date(indexed)'])

df.loc[df['Date(indexed)'] > '2020-01-01','Total Products'] = 0

print(df)

  Date(indexed)  Total Clients  Sales Headcount  Total Products
0    2019-11-01           1005                5               4
1    2019-12-01           1033                5               5
2    2020-01-01           1045               10               6
3    2020-02-01           1124               10               0
4    2020-03-01           1199               10               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.