Home > Net >  How to group together values in Python?
How to group together values in Python?

Time:09-30

I am having trouble creating a count of screen_size values above 6 inches for each brand_name.

The data:

enter image description here

My code thus far:

df.loc[df["screen_size"]>=6.0]

CodePudding user response:

No need for loc

df2 = df[df["screen_size"]>=6.0]
print(df2.shape)

CodePudding user response:

Maybe try:

print((df.loc["screen_size"]>=6.0).count())

CodePudding user response:

if you want to use loc,Maybe try:

df.loc[:,df["screen_size"]>=6.0]

CodePudding user response:

Try:

df.loc[df["screen_size"]>=6.0,"brand_name"].value_counts()

(i) df.loc[df["screen_size"]>=6.0,"brand_name"] is a pandas Series that consists of the rows of the brand_name column where the corresponding screen_size>=6

(ii) value_counts() method counts each brand_name in that pandas Series.

  • Related