Alexis Eggermont Alexis Eggermont - 1 year ago 283
Python Question

DataFrame object has no attribute 'sample'

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

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

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.

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.

What could cause this?

Answer Source

As given in the documentation of 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.

(Emphasis mine).

DataFrame.sample is added in 0.16.1 , you can either -

  1. Upgrade your pandas version to latest, you can use pip for 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)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download