Page 46 - Informatics_Practices_Fliipbook_Class12
P. 46
Item Purchased Bread
Cost 22.5
dtype: object
>>> print('\nPurchase 2:\n',purchase2)
output:
Purchase 2:
Name Nikita
Items Purchased Vegetables
Cost 90.0
dtype: object
>>> print('\nPurchase 3:\n',purchase3)
output:
Purchase 3:
Name Vinod
Item Purchased Milk
Cost 75.0
dtype: object
We make use of the above data in the form of three series (purchase1, purchase2, purchase3) to create a
DataFrame.
>>> import pandas as pd
>>> purchasesDF = pd.DataFrame([purchase1, purchase2, purchase3])
>>> print(purchasesDF)
Name Item Purchased Cost Items Purchased
0 Ashish Bread 22.5 NaN
1 Nikita NaN 90.0 Vegetables
2 Vinod Milk 75.0 NaN
Note that row labels in the series (purchase1, purchase2, purchase3) now serve as column labels.
2.2.3 Creating DataFrame using Lists
Pandas also allows us to create a DataFrame using a list of lists. Each sublist in the list includes the values in a
row of the DataFrame. For example, let us create a nested list comprising information about three purchases made
at the grocery store. To create the DataFrame purchasesDF, we pass the list purchases as an input argument
to pd.DataFrame:
>>> import pandas as pd
>>> purchases = [['Ashish', 'Bread', 22.50], ['Nikita', 'Vegetables', 90.0], ['Vinod',
... 'Milk', 75.0]]
>>> purchasesDF = pd.DataFrame(purchases)
>>> print(purchasesDF)
output:
0 1 2
0 Ashish Bread 22.5
1 Nikita Vegetables 90.0
2 Vinod Milk 75.0
Note that default row and column indexes also known as row and column labels begin with 0. However, use of
meaningful column names, makes it easier to comprehend the data stored in a DataFrame. Pandas allows us to name
the columns explicitly using the keyword argument columns as shown below:
>>> import pandas as pd
>>> purchases = [['Ashish', 'Bread', 22.50], ['Nikita', 'Vegetables', 90.0], ['Vinod',
... 'Milk', 75.0]]
purchasesDF = pd.DataFrame(purchases, columns = ['CustomerName', 'ItemPurchased',
... 'Cost'])
>>> print(purchasesDF)
32 Touchpad Informatics Practices-XII

