mercredi 4 août 2021

Using Python with Pandas to output random rows from two columns

I have a spreadsheet with three columns. I want to output an n number of random rows, and this works for outputting any amount of random rows from one column:

    df = pandas.read_excel(filename, header=0, names=["Speaker","Time","Message"])
    random.choices(df["Message"], k=10)

From what I've read, you should be able to select multiple columns by doing this:

    df = pandas.read_excel(filename, header=0, names=["Speaker","Time","Message"])
    random.choices(df[["Speaker","Message"]], k=10)

But this gives me a keyerror. I'm not sure what I'm missing. Other examples seem to make it pretty straightforward, but I must be missing something, probably extremely simple.

Thanks.




Aucun commentaire:

Enregistrer un commentaire