【问题标题】:A more pythonic way of adding sampled rows to a dataframe and calculating the mean将采样行添加到数据帧并计算平均值的更 Pythonic 方式
【发布时间】:2019-06-05 02:16:09
【问题描述】:

我正在尝试从一个数据框中 CV 10 个随机样本并取平均值,这是我非常基础的版本,感觉非常笨拙。任何人都可以推荐一种更pythonic的方式吗?

graph_data = pd.DataFrame()
for x in range(10):
    y = source_data.loc[filter_1,'attribute'].sample(n=100000)
    y.reset_index(inplace=True, drop=True)
    graph_data[x] = y

graph_data['mean'] = graph_data.mean(axis=1)

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    list comprehensionconcat 一起使用:

    N = 100000
    L=[source_data.loc[filter_1,'attribute'].sample(n=N).reset_index(drop=True) for x in range(10)]
    graph_data = pd.concat(L, axis=1).mean(axis=1).to_frame('mean')
    

    【讨论】:

      猜你喜欢
      • 2021-06-23
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多