Making a date set Random with pandas

Asked

Viewed 1,557 times

0

I’m trying to extract 15O lines from a 500-line dataset. So I’d like to do it on Andom.

My data

objeto,cor,label
cachorro,branco,animal
manga,laranja,fruta
calça,preta,roupa

My script

import pandas
import pandas as pd

df = pd.read_csv('produit_non_conforme.csv', sep = ',')
mails_random = df.sample(150) 

print(mails_random)

But the result is very strange, I don’t have the complete line...

         objeto                ...         label
277      uva                   ...         fruta
116      urso                  ...         animal
495      ...                   ...         ...

It would be possible to have the complete line?

  • 1

    It’s not just a matter of the print hide column by space? Try doing print(mails_random['cor']).

  • Unfortunately when I do this the print only prints that color column. And I need the three...

  • 1

    Try print( pd.DataFrame(df, columns=['objeto', 'cor', 'label']) )

  • 1

    I think this link here: https://stackoverflow.com/questions/11707586/python-pandas-how-to-widen-output-display-to-see-more-columns Can you help

2 answers

1

So. Suppose you want to select two random left onto a 4-line data_frame.

you can proceed as follows:

import numpy as np
import pandas as pd


df = pd.DataFrame({'a':[1,2,3,4],'b':[1,2,3,4]})

df_index = list(df.index)

indexs = np.random.choice(df_index,2)

new_df = df.iloc[indexs]

I hope I’ve helped

1


You could just pick up the values Dataframe and use it as you wish.

Ex.:

import pandas as pd

df = pd.read_csv('teste.csv', sep = ',')
mails_random = df.sample(2) 

for linha in mails_random.values:
    print(linha)  # ['coluna_1', 'coluna_2', 'coluna_3']

Repl.it with the code working

Browser other questions tagged

You are not signed in. Login or sign up in order to post.