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)
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 5
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Suggested prompts:
Kysy minulta kysymyksiä tästä aiheesta
Tiivistä tämä luku
Näytä käytännön esimerkkejä
Awesome!
Completion rate improved to 2.7
The `sample()` Method
Pyyhkäise näyttääksesi valikon
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)
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 3. Luku 5