Pandas: Select first 2 rows, 2 columns and specific two columns of a given dataframe
2. Row and Column Slicing
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
For more Practice: Solve these Related Problems:
- Write a Pandas program to select the first two rows and the last two columns using both loc and iloc methods.
- Write a Pandas program to extract a specific pair of columns along with the top three rows, then rename the extracted columns.
- Write a Pandas program to select a subset of rows and columns based on integer positions and then reorder the columns alphabetically.
- Write a Pandas program to extract two specific columns and slice the first two rows, then verify the result by checking column indices.
Go to:
PREV : Dataset Dimensions.
NEXT :
Random Sampling of Rows.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
