编写一个 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 兄妹拍照祝福语简短的话