0% found this document useful (0 votes)
0 views

chapter 2 Q & A

The document explains the concepts of Series and DataFrame in pandas, highlighting their differences from arrays, lists, and dictionaries. It details how Series are one-dimensional labeled arrays and DataFrames are two-dimensional labeled structures that can hold different data types. Additionally, it provides examples of creating and manipulating Series and DataFrames, along with various operations and methods available in pandas.

Uploaded by

Saily Dhoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

chapter 2 Q & A

The document explains the concepts of Series and DataFrame in pandas, highlighting their differences from arrays, lists, and dictionaries. It details how Series are one-dimensional labeled arrays and DataFrames are two-dimensional labeled structures that can hold different data types. Additionally, it provides examples of creating and manipulating Series and DataFrames, along with various operations and methods available in pandas.

Uploaded by

Saily Dhoke
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. What is a Series and how is it different from a 1-D array, a list and a dictionary?

A Series is a one-dimensional labeled array in pandas capable of holding data of any type.
Difference:
• 1-D Array (NumPy): Homogeneous data, no labels.
• List (Python): Can hold mixed data, no labels.
• Dictionary: Key-value pairs, but unordered (until Python 3.7+).
• Series combines the best of both: it has labels (like a dictionary) and behaves like an array for
computations.

2. What is a DataFrame and how is it different from a 2-D array?


A DataFrame is a 2D labeled data structure with columns of potentially different types.
Difference:
• 2-D array: All elements must be of the same type.
• DataFrame: Can contain columns of different data types, and both rows and columns are labeled.

3. How are DataFrames related to Series?


A DataFrame is essentially a collection of Series objects that share a common index. Each column in a
DataFrame is a Series.

4. What do you understand by the size of:


• Series: Number of elements.
• DataFrame: Total number of elements = rows × columns.

5. Create Series:
import pandas as pd
import numpy as np
import string
# a)EngAlph = pd.Series(list(string.ascii_lowercase))
# b)Vowels = pd.Series(0, index=['a', 'e', 'i', 'o', 'u'])
print(Vowels.empty) # False
# c)Friends = pd.Series({
'Amit': 101,
'Sana': 102,
'Ravi': 103,
'Tina': 104,
'Neha': 105})
# d)MTseries = pd.Series(dtype='float64')
print(MTseries.empty) # True
# e)MonthDays = pd.Series(np.array([31,28,31,30,31,30,31,31,30,31,30,31]), index=range(1,13))

6. Operations on Series:
# a)Vowels[:] = 10
print(Vowels)
# b)Vowels = Vowels / 2
print(Vowels)
# c)Vowels1 = pd.Series([2,5,6,3,8], index=['a','e','i','o','u'])
# d)Vowels3 = Vowels + Vowels1
# e)print(Vowels - Vowels1)
print(Vowels * Vowels1)
print(Vowels / Vowels1)
# f)Vowels1.index = ['A','E','I','O','U']

7. Series Info and Accessing Elements:


for s in [EngAlph, Vowels, Friends, MTseries, MonthDays]:
print(s.ndim, s.size, s.values)
# b)MTseries.name = 'SeriesEmpty'
# c)MonthDays.index.name = 'monthno'
Friends.index.name = 'Fname'
# d)print(Friends.iloc[[2,1]])
# e)print(EngAlph[4:16]) # 'e' to 'p'
# f)print(EngAlph[:10])
# g)print(EngAlph[-10:])
# h)print(MTseries)
8. More Series Operations:
# a)print(MonthDays.loc[3:7])
# b)print(MonthDays[::-1])

9. Create DataFrame Sales:


data = {
2014: [100.5, 150.8, 200.9, 30000, 40000],
2015: [12000, 18000, 22000, 30000, 45000],
2016: [20000, 50000, 70000, 100000, 125000],
2017: [50000, 60000, 70000, 80000, 90000]}
Sales = pd.DataFrame(data, index=['Madhu', 'Kusum', 'Kinshuk', 'Ankit', 'Shruti'])

10. DataFrame Operations:


# a)print(Sales.index)
# b)print(Sales.columns)
# c)print(Sales.dtypes)
# d)print(Sales.ndim, Sales.shape, Sales.size, Sales.values)
# e)print(Sales.tail(2))
# f)print(Sales.iloc[:, :2])
# g)sales_dict = {
'Madhu': 160000,
'Kusum': 110000,
'Kinshuk': 500000,
'Ankit': 340000,
'Shruti': 900000}
Sales2 = pd.DataFrame({'2018': sales_dict})
print(Sales2.empty)

11. Advanced DataFrame Operations:


# a)Sales = pd.concat([Sales, Sales2], axis=1)
# b)Sales = Sales.T
# c)print(Sales.loc[2017])
# d)print(Sales.loc[[2017,2018], ['Madhu','Ankit']])
# e)print(Sales.loc[2016, 'Shruti'])
# f)Sales['Sumeet'] = [196.2, 37800, 52000, 78438, 38852]
# g)Sales = Sales.drop(2014)
# h)Sales = Sales.drop('Kinshuk', axis=1)
# i)Sales = Sales.rename(columns={'Ankit':'Vivaan', 'Madhu':'Shailesh'})
# j)Sales.loc[2018, 'Shailesh'] = 100000
# k)Sales.to_csv('SalesFigures.csv', header=False, index=False)
# l)SalesRetrieved = pd.read_csv('SalesFigures.csv', header=None)
SalesRetrieved.index = Sales.index
SalesRetrieved.columns = Sales.columns

You might also like