Page 45 - Informatics_Practices_Fliipbook_Class12
P. 45
>>> import pandas as pd
>>> accountsData = {'Date': ['2022-01-01', '2022-01-02', '2022-01-03', '2022-01-04',
... '2022-01-05'],'Sales': [10000, 20000, 15000, 12000, 13000],
... 'Expenses': [5000, 7500, 6000, 8000, 9000]}
Now we convert the above dictionary to a DataFrame using the method pd.DataFrame():
>>> accountsDF = pd.DataFrame(accountsData)
>>> print(accountsDF)
output:
Date Sales Expenses
0 2022-01-01 10000 5000
1 2022-01-02 20000 7500
2 2022-01-03 15000 6000
3 2022-01-04 12000 8000
4 2022-01-05 13000 9000
Note that the keys of the dictionary act as column labels. For each key (acting as a label), the values in the list appear
in the corresponding column in the DataFrame. Thus, the resulting DataFrame has three columns: Date, Sales, and
Expenses as shown above. Note that row indexes are labeled as 0, 1, 2, ... .
Let us consider the example of a grocery shop. The shop owner wants to keep track of the following details of the
purchases made by the cutomers: customer's name, item purchased, and the cost of purchase. Currently, this
information is stored in the dictionary purchases. To facilitate further analysis, we transform this dictionary into the
Pandas DataFrame (puchasesDF):
>>> import pandas as pd
>>> purchases = {'CustomerName':['Ashish', 'Nikita', 'Vinod'],
'ItemPuchased':['Bread', 'Vegetables', 'Milk'],
'Cost':[22.50, 90.00, 75.00]}
>>> purchasesDF = pd.DataFrame(purchases)
>>> print(purchasesDF)
output:
CustomerName ItemPuchased Cost
0 Ashish Bread 22.5
1 Nikita Vegetables 90.0
2 Vinod Milk 75.0
Note that the text in the columns- CustomerName and ItemPurchased is right-aligned. This is the default style in
Pandas.
2.2.2 Creating DataFrame using Series
While constructing the purchasesDF, we assumed that the information is available in the form of a dictionary. Now
let us suppose that the information about each purchase is available in the form of a series as shown below:
>>> import pandas as pd
>>> purchase1 = pd.Series({'Name': 'Ashish',
'Item Purchased': 'Bread',
'Cost': 22.50})
>>> purchase2 = pd.Series({'Name': 'Nikita',
'Items Purchased': 'Vegetables',
'Cost': 90.00})
>>> purchase3 = pd.Series({'Name': 'Vinod',
'Item Purchased': 'Milk',
'Cost': 75.00})
>>> print('\nPurchase 1:\n',purchase1)
output:
Purchase 1:
Name Ashish
Data Handling using Pandas DataFrame 31

