CBSE Class 12 Informatics Practices Question 39 of 40

Python Pandas — I — Question 15

Back to all questions
15
Question

Question 11

Write a program to create three different Series objects from the three rows of a DataFrame df.

Solution
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
s1 = df.iloc[0]
s2 = df.iloc[1]
s3 = df.iloc[2]
print(s1)
print(s2)
print(s3)
Output
A    1
B    4
C    7
Name: 0, dtype: int64
A    2
B    5
C    8
Name: 1, dtype: int64
A    3
B    6
C    9
Name: 2, dtype: int64
Answer

import
pandas
as
pd
df
=
pd
.
DataFrame
({
'A'
: [
1
,
2
,
3
],
'B'
: [
4
,
5
,
6
],
'C'
: [
7
,
8
,
9
]})
s1
=
df
.
iloc
[
0
]
s2
=
df
.
iloc
[
1
]
s3
=
df
.
iloc
[
2
]
print
(
s1
)
print
(
s2
)
print
(
s3
)
Output
A 1
B 4
C 7
Name: 0, dtype: int64
A 2
B 5
C 8
Name: 1, dtype: int64
A 3
B 6
C 9
Name: 2, dtype: int64