Header Ads Widget

Responsive Advertisement

Class 12 code : 065 | DataFrame in python with example


Class 12 subject code : 065 , DataFrame in python with example

Introduction :DataFrame is another Pandas Structure which stores data in two-Dimensional way. It is actually a two-Dimensional labelled array. which is actually an ordered collection of columns where columns may store different type of data. 


Characteristics of a DataFrame Data Structure :

  • It has tow indexes or we can say that two axes - a row index (axis=0) and a column index(axis=1).
  • Conceptually it is like a spreadsheet where each value is identifiable with the combination of row index and column index..The row index is known as index in general and the column index i called the column-name.
  • The indexes can be of numbers of letters or strings.
  • There is no condition of having all data of same type across columns; its columns can have data of different types.
  • You can easily change its values, It is values-mutable.
  • You can add or delete row /columns in a Dataframe. In other words , it is size mutable.

Creating and displaying a DataFrame

A DataFrame object can be created by passing data in 2D format. Like earlier, before you do anything with pandas module, make sure to import pandas and numpy modules.

import pandas as pd

import numpy as pd

Create DataFrame object, you can use syntax as

DataFrane Object name=pd.DataFrame()   # empty dataframe

import pandas as pd

import numpy as pd 

DF1=pd.DataFrame()

 



Creating a dataframe from a 2D diction having values as lists/ndarray: 

import pandas as pd 

dict={"Student":["Ram","Shyam","Neha","Gurjyot"], "Marks":[80,98,89,85],"Age":[15,14,16,17]}

df1=pd.DataFrame(dict1)

print(df1)

           Student     Marks   Age

    0     Ram          80         15

    1     Shyam       98         14

    2     Neha         89         16

    3     Gurjyot     85         17


Nested Dictionary can be use to create dataframe in pandas, In this dictionary keys of the inner dictionaries make the indexes and the keys of outer dictionary make the columns. Let us create a data frame using nested dictionary. 
import pandas as pd
sales={"A" : {"AA": 11,"BB":12,"CC":13},  
"B" : {"AA": 21,"BB":22,"CC":23}, 
"C":{"AA": 31,"BB":32,"CC":33}}
nesteddict=pd.DataFrame(sales)
print(nesteddict)



 Nested list with dissimilar keys

import pandas as pd
sales={"A" : {"AA": 11,"BB":12,"CC":13},
       "B" : {"AA": 21,"EE":22,"CC":23},
       "C":{"AA": 31,"BB":32,"DD":33}}
nesteddict=pd.DataFrame(sales)
print(nesteddict)

 


Creating a DataFrame object from a list of Dictionary/List

import pandas as pd

class_10A={"Roll No": 11, "Name":"Rajan","Marks":99}

class_10B={"Roll No": 12, "Name":"Raju","Marks":95}

class_10C={"Roll No": 11, "Name":"Rajender","Marks":96}

boardresutl=[class_10A,class_10B,class_10C]

df=pd.DataFrame(boardresult)

print(df)



Assignment No 1

1.Write a program to created a dataframe from a list containing dictionary of the sales performance of four zonal offices. Zone names should be the row labels.

2. Write a program create a dataframe from a list containing 2 lists, each containing Target and actual sales figures of four zonal offices. Give appropriate row labels.




Create a dataframe from a 2 D list.

import pandas as pd

list1=[[25,45,63,47],[25,14,85,47],[96,52,85,47]]

df1=pd.DataFrame(list2,index=['row1','row2','row3'])

print(df1)


Creating a DataFrame Object from a 2D dictionary with Values as Series Objects.

import pandas as pd 

s1=pd.Series([20,30,40])

s2=pd.Series([1000,2000,3000])

school={"EMPID": s1,"Salary Per Day":s2}

df1=pd.DataFrame(school)

print(df1)


We can create DataFrame object from another DataFrame Object 

import pandas as pd 

s1=pd.Series([20,30,40])

s2=pd.Series([1000,2000,3000])

school={"EMPID": s1,"Salary Per Day":s2}

df1=pd.DataFrame(school)

dfnew=pd.DataFrame(df1)

print(dfnew)




Click Here to check Data Frame Attributes 



What is DataFrame in Python Class 12? What is DataFrame explain? What is DataFrame explain with example? How are DataFrames related to Series 12? python pandas class 12 notes pdf dataframe class 12 questions and answers data handling using pandas 2 notes pandas stands for data handling using pandas 1 notes python pandas 1 notes python pandas project class 12 pandas series notes

Post a Comment

0 Comments