Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

df.sample(ignore_index=True) still returns index

How do I sample() a row from a Dataframe, without its index?

If there is a different approach, that’s ok.


Pandas documentation offers ignore_index parameter.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Source

However, when I run:

df['col'].sample(ignore_index=True)

I’ll still get the index and the value, e.g.:

1    value

Desired:

value

>Solution :

In pandas all objects like Series and DataFrames has index.

Parameter ignore_index obviously generate default RangeIndex, not remove it.

If need scalar from Series select first value:

out = df['col'].sample(1).iat[0]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading