You can examine a sample of a DataFrame by using the .sample() method. This method returns a random sample of rows from the DataFrame.
Here's an example:
import pandas as pddf = pd.read_csv('my_data.csv')# examine a sample of 5 rowssample = df.sample(5)print(sample) |
In this example, we first read in a CSV file called my_data.csv using the pd.read_csv() method and assign the resulting DataFrame to a variable called df.
We can then examine a sample of 5 rows from the DataFrame by calling the .sample() method with an argument of 5. This will return a new DataFrame containing 5 random rows from the original DataFrame.
Finally, we print the sample to the console using the print() function.
You can adjust the number of rows returned by changing the argument passed to the .sample() method. You can also pass a random_state argument to the method to ensure that the same sample is returned each time the code is run.