简体   繁体   English

如何将 Python 中的值组合在一起?

[英]How to group together values in Python?

I am having trouble creating a count of screen_size values above 6 inches for each brand_name.我在为每个 brand_name 创建超过 6 英寸的 screen_size 值计数时遇到问题。

The data:数据:

在此处输入图像描述

My code thus far:到目前为止我的代码:

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

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 (i) df.loc[df["screen_size"]>=6.0,"brand_name"]是一个 pandas 系列,由brand_name列的行组成,其中相应的screen_size>=6

(ii) value_counts() method counts each brand_name in that pandas Series. (ii) value_counts()方法计算该brand_name系列中的每个品牌名称。

No need for loc不需要 loc

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

Maybe try:也许尝试:

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

if you want to use loc,Maybe try:如果你想使用 loc,也许可以尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM