1

I would like create a new column my dataset and insert a value in a specific row based ona few conditions.

I found this previous post that was helpful but it does not account for multiple conditions, just name == Tim. How to create a new column and insert value at a particular row in pandas dataframe?

DF:

Date Group State Count
2021-01-01 001 IL 0
2021-01-01 002 IL 5
2021-01-02 001 CA 3
2021-01-02 002 CA 9
2021-01-03 001 AZ 2
2021-01-03 002 AZ 8
2021-01-04 001 WY 4
2021-01-04 002 WY 7

Desired Output:

DF:

Date Group State Count Sub
2021-01-01 001 IL 0 0
2021-01-01 002 IL 5 0
2021-01-02 001 CA 3 0
2021-01-02 002 CA 9 0
2021-01-03 001 AZ 2 0
2021-01-03 002 AZ 8 3
2021-01-04 001 WY 4 0
2021-01-04 002 WY 7 0

I would like insert the value '3' where Date == 2021-01-03 and group = 002 and state = 'AZ'.

EDIT: I cannot use the post that was linked to this question because the rows of this dataframe are updated everyday. As such, I need it to pinpoint the exact day, group, and state, not just the row number.

Solution found:

df.loc[(df['Date'] == '2021-01-03') & (df['Group'] == '002') & (df['State'] == 'AZ'), 'Sub'] = 3

Any help would be appreciated.

Trevor M
  • 89
  • 9

0 Answers0