Home > Net >  How to Eliminate FOR Loop with the extraction of each value from <class 'pandas.core.series.
How to Eliminate FOR Loop with the extraction of each value from <class 'pandas.core.series.

Time:03-05

Unable to Extract correct values from Pandas Series to use in np.select Function as default value. # With For Loop: Getting Perfect values in column "C" ["No","No",4,4,4,4,4,4]

df = pd.DataFrame()
df["A"] = ["No","No","Max","Max","Min","Max","No","No"]
df["B"] = [5,9,4,3,7,6,8,1]
df["C"] = "No"
for i in range(1,len(df)-1):
    if (df.iloc[i, 0]=="No") & ((df.iloc[1 i, 0]=="Max") or (df.iloc[1 i, 
    0]=="Min")):
        df.iloc[1 i, 2] = df.iloc[1 i, 1]
    else:
        df.iloc[1 i, 2] = df.iloc[i, 2]
df

'''

# Without for Loop : Getting wrong values ["No","No",4,"No","No","No","No","No"] in 
Column "C" instead of ["No","No",4,4,4,4,4,4].
df = pd.DataFrame()
df["A"] = ["No","No","Max","Max","Min","Max","No","No"]
df["B"] = [5,9,4,3,7,6,8,1]
df["C"] = None

A = (df["A"].shift(1).fillna("No").astype(str))
C = (df["C"].shift(1).fillna("No").astype(str))
conditions = [((A == "No") & ((df["A"].values == "Max") | (df["A"].values == 
    "Min")))]
choices = [df["B"]]
df["C"] = np.select(conditions, choices, default = C)
df

'''

# How to get The Perfect Values in column "C" without using For loop. 
# Unable to extract individual values from <class 'pandas.core.series.Series'> which 
  is defined as variable C.

Perfect Output with For Loop in Column "C"

Imperfect Values in Column C with np.select function instead of for loop

CodePudding user response:

You code is equivalent to:

m1 = df['A'].shift(1).eq('No')    # previous row is No
m2 = df['A'].isin(['Min', 'Max']) # current row is Min or Max
#         take B if both masks / ffill / replace NaN with original C
df['C'] =  df['B'].where(m1&m2).ffill().fillna(df['C'])

output:

     A  B    C
0   No  5   No
1   No  9   No
2  Max  4  4.0
3  Max  3  4.0
4  Min  7  4.0
5  Max  6  4.0
6   No  8  4.0
7   No  1  4.0

NB. If you want to keep the object type, use: df['C'] = df['B'].where(m1&m2).ffill().convert_dtypes().astype(object).fillna(df['C'])

  • Related