How to get the values from a cluster with column name "crashloc", where "crashtype"=="Serious Suspected Injury"

Viewed 210

I have a pandas dataframe where I have two columns named "crashtype" and "crashloc" I have applied K-means clustering and created a new column named "Clusters" to store the cluster values. Now I want to print the values of cluster 1 "crashloc" where the "crahtype"=="Suspected Serious Injury" How to do that?

2 Answers

Here is the answer: First select the rows that meet your condition

 df1 = df.loc[df.crahtype =='Suspected Serious Injury']

Then you could print value on any column you want

print(df1["Clusters"])

Actually I solved my problem like this

df1 = df.loc[df["crashtype"] =='Suspected Serious Injury']
df2 = df1.loc[df1["CLUSTERS"] ==1]
print(df2["crashloc"].value_counts())

Output is-

Non-Intersection        72
Non-Roadway             24
Intersection            12
Intersection-Related     5
Name: crashloc, dtype: int64

So I have 10 clusters labeled 0-9 and I wanted to extract the values of "crashloc" from Cluster labeled 1 where the "crashtype"=='Suspected Serious Injury' and this is how I did it :)

Related