简体   繁体   中英

how to use if statement with multiple conditions on pandas dataframe

current df is,

YEAR | MONTH
2016 | 1     
2016 | 2     
...
...
2020 | 4     
2020 | 5     
...
...
2021 | 1     
2021 | 2     

Output should be based on 2 criteria:

  1. if YEAR <= 2021 and MONTH < 7 should be "History"
  2. if YEAR >= 2021 and MONTH >= 7 should be "Forecast"
YEAR | MONTH | TIME_TYPE
2016 | 1     | History
2016 | 2     | History
...
...
2020 | 4     | History
2020 | 5     | History
...
...
2021 | 1     | History
2021 | 2     | History
2021 | 3     | History
2021 | 4     | History
2021 | 5     | History
2021 | 6     | History
2021 | 7     | Forecast
2021 | 8     | Forecast
2021 | 9     | Forecast
2021 | 10    | Forecast

A np.where option by creating a datetime column with to_datetime from the YEAR and MONTH columns and filtering values before 2021-07 :

import numpy as np
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({'YEAR': [2020] * 3 + [2021] * 12,
                   'MONTH': list(range(10, 13)) + list(range(1, 13))})

# Create DateTime Series from Year and Month (Adding DAY value)
s = pd.to_datetime(df[['YEAR', 'MONTH']].assign(DAY=1))
# Creating New Column filtering before 2021-07
df['TIME_TYPE'] = np.where(s < '2021-07', 'History', 'Forecast')

print(df)

Or for current month:

df['TIME_TYPE'] = np.where(
    s < pd.Timestamp('today').strftime('%Y-%m'),
    'History', 'Forecast'
)

df :

    YEAR  MONTH TIME_TYPE
0   2020     10   History
1   2020     11   History
2   2020     12   History
3   2021      1   History
4   2021      2   History
5   2021      3   History
6   2021      4   History
7   2021      5   History
8   2021      6   History
9   2021      7  Forecast
10  2021      8  Forecast
11  2021      9  Forecast
12  2021     10  Forecast
13  2021     11  Forecast
14  2021     12  Forecast

Try This:

import  pandas as pd

data = {
    "YEAR" : [2016,2016,2020,2020,2021,2021,2021,2021,2021,2021,2021,2021,2021,2021],
    "MONTH" : [1,2,4,5,1,2,3,4,5,6,7,8,9,10]
}

df = pd.DataFrame(data)
df.loc[(df["YEAR"] <= 2021) & (df["MONTH"] < 7),"TIME_TYPE"] = "History" 
df.loc[(df["YEAR"] >= 2021) & (df["MONTH"] >= 7),"TIME_TYPE"] = "Forecast" 
print(df)

Result:

    YEAR  MONTH TIME_TYPE
0   2016      1   History
1   2016      2   History
2   2020      4   History
3   2020      5   History
4   2021      1   History
5   2021      2   History
6   2021      3   History
7   2021      4   History
8   2021      5   History
9   2021      6   History
10  2021      7  Forecast
11  2021      8  Forecast
12  2021      9  Forecast
13  2021     10  Forecast

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM