如何通过从 <class 'pandas.core.series.Series'> 中提取每个值来消除 FOR 循环
How to Eliminate FOR Loop with the extraction of each value from <class 'pandas.core.series.Series'>
无法从 Pandas 系列中提取正确的值以在 np.select 函数中用作默认值。
# 使用 For 循环:在“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.
您的代码相当于:
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'])
输出:
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
注意。如果要保留 object 类型,请使用:df['C'] = df['B'].where(m1&m2).ffill().convert_dtypes().astype(object).fillna(df['C'])
无法从 Pandas 系列中提取正确的值以在 np.select 函数中用作默认值。 # 使用 For 循环:在“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.
您的代码相当于:
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'])
输出:
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
注意。如果要保留 object 类型,请使用:df['C'] = df['B'].where(m1&m2).ffill().convert_dtypes().astype(object).fillna(df['C'])