gpt4 book ai didi

pandas - Pandas 中 mode() 的含义

转载 作者:行者123 更新时间:2023-12-04 04:15:20 26 4
gpt4 key购买 nike

df5 = pd.DataFrame({"A": np.random.randint(0, 7, size=50),
"B": np.random.randint(-10, 15, size=50)})

df5.mode()


A B
0 1.0 -9

1 NaN 10

2 NaN 13



为什么 NaN 来自这里?

最佳答案

原因是如果检查 DataFrame.mode :

Get the mode(s) of each element along the selected axis.

The mode of a set of values is the value that appears most often. It can be multiple values.


所以缺失值意味着 A是一个 mode值,对于 B列是 3 个模式值,因此对于相同的行添加缺失值。
如果检查我的样本数据 - 有模式 A 2次和 B只有一次,因为 23都是 11数据中的时间:
np.random.seed(20)
df5 = pd.DataFrame({"A": np.random.randint(0, 7, size=50),
"B": np.random.randint(-10, 15, size=50)})

print (df5.mode())
A B
0 2 8.0
1 3 NaN
print (df5.A.value_counts())
3 11 <- both top1
2 11 <- both top1
6 9
5 8
0 5
1 4
4 2
Name: A, dtype: int64

print (df5.B.value_counts())
8 6 <- only one top1
0 4
4 4
-4 3
10 3
-2 3
1 3
12 3
6 3
7 2
3 2
5 2
-9 2
-6 2
14 2
9 2
-1 1
11 1
-3 1
-7 1
Name: B, dtype: int64

关于pandas - Pandas 中 mode() 的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60797080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com