The `sample()` Method
The sample()
method returns a random row from the DataFrame by default.
There are two optional named parameters which can be used to specify how many random rows to fetch:
n
: Specifies the exact number of random rows to fetch;frac
: Specifies the fraction or percentage of the total rows to fetch. For example,frac=0.2
will return 20% of the total rows;
123456789101112import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Coffee': ['Espresso', 'Latte', 'Cappuccino', 'Americano', 'Macchiato'], 'Price': [2.5, 3.5, 3.0, 2.8, 3.2], 'Availability': ['Yes', 'Yes', 'Yes', 'Yes', 'No'] }) print("Get 2 random rows from the DataFrame:") random_rows = df.sample(n=2) print(random_rows)
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 5
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione
Suggested prompts:
Mi faccia domande su questo argomento
Riassuma questo capitolo
Mostri esempi dal mondo reale
Awesome!
Completion rate improved to 2.7
The `sample()` Method
Scorri per mostrare il menu
The sample()
method returns a random row from the DataFrame by default.
There are two optional named parameters which can be used to specify how many random rows to fetch:
n
: Specifies the exact number of random rows to fetch;frac
: Specifies the fraction or percentage of the total rows to fetch. For example,frac=0.2
will return 20% of the total rows;
123456789101112import pandas as pd # Sample DataFrame df = pd.DataFrame({ 'Coffee': ['Espresso', 'Latte', 'Cappuccino', 'Americano', 'Macchiato'], 'Price': [2.5, 3.5, 3.0, 2.8, 3.2], 'Availability': ['Yes', 'Yes', 'Yes', 'Yes', 'No'] }) print("Get 2 random rows from the DataFrame:") random_rows = df.sample(n=2) print(random_rows)
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 3. Capitolo 5