how to create dataframe in Python using pandas
In this tutorial, we learn how to create a dataframe in Python using pandas, for this, we have to learn what is Pandas data frame.
In this, we can write a program with the help of the list and dictionary method as we can see in program.
There are multiple ways to create a dataframe now we can see here that way.
1st way to create DataFrame in Python using pandas
# Import Pandas library import pandas as pd Car = { 'Brand':['Honda_Civic','Toyota_Innova','Maruti_Dzire','Sunny_Nissan'], 'Year':[2009,2007,2015,2014], 'Price':[220000,270000,450000,300000] } # Create pandas data frame df=pd.DataFrame(Car,columns=['Brand','Year','Price']) # print dataframe print(df)
output: Brand Year Price
0 Honda_Civic 2009 220000
1 Toyota_Innova 2007 270000
2 Maruti_Dzire 2015 450000
3 Sunny_Nissan 2014 300000
Explanation:
In this Program, we can Import the Pandas Library after that we can taking data in car objects and after that making DataFrame and print Car Data in Frame formate.
2nd way to create DataFrame.
import pandas as pd Detail = [['Raj',25],['Vijay',30],['Khushi',20]] df= pd.DataFrame(Detail, columns=['Name','Age']) print(df)
output: Name Age 0 Raj 25 1 Vijay 30 2 Khushi 20
Explanation: In this program, we can import pandas library after that we can taking data in Detail object and after that making DataFrame and print Detail data in frame formate.
3rd way to create DataFrame
import pandas as pd Detail = {'Name':['Vijay','Raj','Khushi'],'Percentage':[97,78,67]} df=pd.DataFrame(Detail,index=['rank1','rank2','rank3']) print(df)
output: Name Percentage
rank1 Vijay 97
rank2 Raj 78
rank3 Khushi 67
Explanation: In this program, we can import pandas library after that taking Detail object to take data of name and percentage and take that data in a dataframe with rank and print that data in the frame.
Also read:
Leave a Reply