pandas DataFrame 对象没有属性“样本”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33251320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:05:18  来源:igfitidea点击:

DataFrame object has no attribute 'sample'

pythonpandasmodule

提问by Alexis Eggermont

Simple code like this won't work anymore on my python shell:

像这样的简单代码将不再适用于我的 python shell:

import pandas as pd
df=pd.read_csv("K:/01. Personal/04. Models/10. Location/output.csv",index_col=None)
df.sample(3000)

The error I get is:

我得到的错误是:

AttributeError: 'DataFrame' object has no attribute 'sample'

DataFrames definitely have a sample function, and this used to work. I recently had some trouble installing and then uninstalling another distribution of python. I don't know if this could be related.

DataFrames 肯定有一个示例函数,这曾经是有效的。我最近在安装然后卸载另一个 python 发行版时遇到了一些麻烦。不知道会不会有这个关系。

I've previously had a similar problem when trying to execute a script which had the same name as a module I was importing, this is not the case here, and pandas.read_csv is actually working.

我以前在尝试执行与我正在导入的模块同名的脚本时遇到过类似的问题,这里不是这种情况,pandas.read_csv 实际上正在工作。

What could cause this?

什么可能导致这种情况?

回答by Anand S Kumar

As given in the documentation of DataFrame.sample-

文档中所述DataFrame.sample-

DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)

Returns a random sample of items from an axis of object.

New in version 0.16.1.

DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)

从对象轴返回项目的随机样本。

0.16.1 版中的新功能。

(Emphasis mine).

(强调我的)。

DataFrame.sampleis added in 0.16.1, you can either -

DataFrame.sample添加到 中0.16.1,您可以 -

  1. Upgrade your pandasversion to latest, you can use pipfor that, Example -

    pip install pandas --upgrade
    
  2. Or if you don't want to upgrade, and want to sample few rows from the dataframe, you can also use random.sample(), Example -

    import random
    num = 100 #number of samples
    sampleddata = df.loc[random.sample(list(df.index),num)]
    
  1. 将您的pandas版本升级到最新版本,您可以使用pip它,例如 -

    pip install pandas --upgrade
    
  2. 或者,如果您不想升级,并想从数据框中采样几行,您也可以使用random.sample(),例如 -

    import random
    num = 100 #number of samples
    sampleddata = df.loc[random.sample(list(df.index),num)]