In this program I'd like to randomly choose one word from a json file
[My code]
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.