编写一个 Python 程序来统计一个 DataFrame 中 20 到 30 岁之间的总年龄数
输入-
假设你有一个DataFrame,
Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18
输出-
Total number of age between 20 to 30 is 2.
解决方案
为了解决这个问题,我们将遵循以下方法。
定义数据帧
将DataFrameAge列设置在20,30之间。将其存储在结果DataFrame中。它定义如下,
df[df['Age'].between(20,30)]
最后,计算结果的长度。
例子
让我们看看下面的实现,以便更好地理解。
import pandas as pd
data = {'Id':[1,2,3,4,5],'Age':[21,23,32,35,18]}
df = pd.DataFrame(data)
print(df)
print("Count the age between 20 to 30")
result = df[df['Age'].between(20,30)]
print(len(result))输出
Id Age 0 1 21 1 2 23 2 3 32 3 4 35 4 5 18 Count the age between 20 to 30 2
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短