gpt4 book ai didi

python - 按字符串过滤,然后找到另一列的方差

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

我使用 pandas 读取我的 csv 文件,然后只读取这两列

  • 描述文件

  • 数字

Describe_File   numbers
0 This is the start 25
1 Ending is coming 42
2 Middle of the story 525
3 This is the start 65
4 This is the start 25
5 Middle of the story 35
6 This is the start 28
7 This is the start 24
8 Ending is coming 24
9 Ending is coming 35
10 Ending is coming 25
11 Ending is coming 24
12 This is the start 215

所以我现在按字符串名称过滤 ** This is the start** 看起来像这样

df = df[df.Describe_File == "这是开始"]

    Describe_File   numbers
0 This is the start 25
3 This is the start 65
4 This is the start 25
6 This is the start 28
7 This is the start 24
12 This is the start 21

现在我只是找到方差np.var(df)

目标

转到 Describe_File 按所有唯一字符串进行过滤,然后找出该字符串的方差和标准差。

输出文件应该是这样的

enter image description here

最佳答案

如您所知,标准差是方差的平方根。所以以下将是最快的方法。

import pandas as pd
import numpy as np

df_out = df.groupby('Describe_File').apply(np.var)
df_out.columns = ['variance']
df_out['standard_deviation'] = np.sqrt(df_out['variance'])

关于python - 按字符串过滤,然后找到另一列的方差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60466076/

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