Pandas: Select first 2 rows, 2 columns and specific two columns of a given dataframe
Pandas Filter: Exercise-2 with Solution
Write a Pandas program to select first 2 rows, 2 columns and specific two columns from World alcohol consumption dataset.
Test Data:
Year WHO region Country Beverage Types Display Value 0 1986 Western Pacific Viet Nam Wine 0.00 1 1986 Americas Uruguay Other 0.50 2 1985 Africa Cte d'Ivoire Wine 1.62 3 1986 Americas Colombia Beer 4.27 4 1987 Americas Saint Kitts and Nevis Beer 1.98
Sample Solution:
Python Code :
import pandas as pd
# World alcohol consumption data
w_a_con = pd.read_csv('world_alcohol.csv')
print("World alcohol consumption sample data:")
print(w_a_con.head())
print("\nSelect first 2 rows:")
print(w_a_con.iloc[:2])
print("\nSelect first 2 columns:")
print(w_a_con.iloc[:,:2].head())
print("\nSelect 2 specific columns:")
print(w_a_con[['Display Value', 'Year']])
Sample Output:
World alcohol consumption sample data: Year WHO region ... Beverage Types Display Value 0 1986 Western Pacific ... Wine 0.00 1 1986 Americas ... Other 0.50 2 1985 Africa ... Wine 1.62 3 1986 Americas ... Beer 4.27 4 1987 Americas ... Beer 1.98 [5 rows x 5 columns] Select first 2 rows: Year WHO region Country Beverage Types Display Value 0 1986 Western Pacific Viet Nam Wine 0.0 1 1986 Americas Uruguay Other 0.5 Select first 2 columns: Year WHO region 0 1986 Western Pacific 1 1986 Americas 2 1985 Africa 3 1986 Americas 4 1987 Americas Select 2 specific columns: Display Value Year 0 0.00 1986 1 0.50 1986 2 1.62 1985 3 4.27 1986 4 1.98 1987 5 0.00 1987 6 0.13 1987 7 0.39 1985 8 1.55 1986 9 6.10 1984 10 0.20 1987 11 0.62 1989 12 0.00 1985 13 0.00 1984 14 0.05 1985 15 0.07 1987 16 0.06 1984 17 2.23 1989 18 1.62 1984 19 1.08 1984 20 0.00 1986 21 4.51 1989 22 2.67 1984 23 0.44 1984 24 NaN 1985 25 0.00 1984 26 1.36 1985 27 2.22 1984 28 0.11 1987 29 NaN 1986 .. ... ... 70 1.02 1986 71 0.57 1985 72 0.00 1987 73 0.01 1986 74 2.06 1986 75 0.00 1989 76 0.02 1985 77 0.01 1985 78 0.00 1989 79 2.09 1989 80 0.84 1985 81 2.54 1985 82 2.25 1987 83 NaN 1986 84 0.00 1986 85 0.01 1985 86 1.83 1986 87 0.01 1989 88 0.42 1987 89 0.70 1986 90 0.01 1989 91 4.43 1989 92 0.00 1986 93 NaN 1987 94 3.06 1985 95 0.00 1984 96 7.38 1985 97 0.00 1984 98 0.00 1984 99 0.00 1985 [100 rows x 2 columns]
Click to download world_alcohol.csv
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:Write a Pandas program to display the dimensions or shape of the World alcohol consumption dataset. Also extract the column names from the dataset.
Next: Write a Pandas program to select random number of rows, fraction of random rows from World alcohol consumption dataset.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/pandas/filter/pandas-filter-exercise-2.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics