Home > Blockchain >  How to read 1 record from a json file using panda
How to read 1 record from a json file using panda

Time:09-01

In this program I'd like to randomly choose one word from a json file

[My code]

1

I succesfully opend the file but i don't know how to access only one record inside "randwords" Thanks!!!

CodePudding user response:

Try this:

randwords.sample(1)

See: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sample.html

CodePudding user response:

You can access a pandas dataframe (the pd) with an operation called indexing. For example pd['data'] will let you have access to the data column. Refer to here for more information.
One specific function that you can benefit here is iloc. For example pd.iloc[0] will let you have access to the first row. Then you can specify which column you are interested int by calling the appropriate column name.

pd.idloc[0].data

This will return the data column of the first row. Using a random number instead of 0 will result in a random row obioulsy.

  • Related