【问题标题】:Use of sample and seed in numpy在 numpy 中使用样本和种子
【发布时间】:2020-08-28 06:22:58
【问题描述】:
die = pd.DataFrame([1, 2, 3, 4, 5, 6])

sum_of_dice = die.sample(n=2, replace=True).sum().loc[0]

print (sum_of_dice)

有人可以解释一下.sum().loc[0] 在这里做什么吗?

【问题讨论】:

  • 嗨,摇滚卡拉汉。您可以更新问题中的标签以包含您使用的语言吗?我最好的猜测是 Python。此外,您正在使用的任何库导入也会很棒。

标签: python pandas sample pandas-loc


【解决方案1】:

打印中间步骤以获得想法总是有用的。

sum 计算每列数据帧的总和。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sum.html

loc 选择一组行/列。

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.loc.html

sum 返回一个包含一个元素的数据帧,但由于我们需要整数 dtype 中的和而不是数据帧,所以我们使用 loc 来获取第一个元素。

import pandas as pd

die = pd.DataFrame([1, 2, 3, 4, 5, 6])

sum_of_dice = die.sample(n=2, replace=True)

print(sum_of_dice)

sum_of_dice = sum_of_dice.sum()

print('---')
print (sum_of_dice)

sum_of_dice = sum_of_dice.loc[0]
print('---')
print (sum_of_dice)
  0
4  5
0  1
---
0    6
dtype: int64
---
6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-10
    • 2016-01-17
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多