3 回答
TA贡献1843条经验 获得超7个赞
使用.groupbyand.transpose在此代码的末尾。
df1 = pd.cut(df['Daily Risk Score'],
bins=[0, 9, 17, np.inf],
labels=['Green','Orange','Red']).reset_index(). \
groupby('Daily Risk Score').count().transpose()
df1
输出:
Daily Risk Score Green Orange Red
index 3 4 2
TA贡献1827条经验 获得超8个赞
尝试:
df1=df.groupby(pd.cut(df['Daily Risk Score'], bins=[0, 9, 17, np.inf], labels=['Green','Orange','Red'])).size()
df1:
Daily Risk Score
Green 3
Orange 5
Red 2
dtype: int64
或者
df1=df.groupby(pd.cut(df['Daily Risk Score'], bins=[0, 9, 17, np.inf], labels=['Green','Orange','Red'])).size()
df2 = pd.DataFrame(df1.reset_index().values.T)
df2.columns = df2.iloc[0]
df2 = df2[1:]
df2:
Green Orange Red
1 3 5 2
TA贡献1859条经验 获得超6个赞
我尝试了一些不同的方法。这也很容易。尝试一下,如果您遇到任何问题/错误,请告诉我。干得好:
df["col"] = 0
for i in range(len(df)):
if 0<df["Daily Risk Score"][i]<9:
df["col"][i] = "0<Daily Risk Score<9"
elif 9<df["Daily Risk Score"][i]<17:
df["col"][i] = "9<Daily Risk Score<17"
elif 9<df["Daily Risk Score"][i]<17:
df["col"][i] = "Daily Risk Score>=17"
else:
df["col"][i] = "other"
df["col"].value_counts()
df.drop(columns=["col"])
添加回答
举报