Creating Pandas Dataframe from Lists and Dictionary Objects

In post, we’ll learn to create pandas dataframe from python lists and dictionary objects. Creating pandas dataframe is fairly simple and basic step for Data Analysis. There are also other ways to create dataframe (i.e. from csv, excel files or even from databases queries). But we’ll cover other steps in other posts.

Import necessary packages

Creating Pandas DataFrame from dictionary object

COUNTRYQ1Q2Q3Q4
0US10.09.57.612.0
1UK11.23.86.99.0
2India9.67.38.311.0
3Singapore9.05.66.910.0

Let’s say we have the same above data but in the form of list of dictionaries.

ISSUEPRIORITYSTATUSOPEN_DATECLOSE_DATE
0PD-1023HighOpen2018-01-03NaN
1PD-1162HighClosed2018-02-052018-02-07
2PD-1231MediumClosed2018-02-272018-03-02
3PD-1345LowOpen2018-03-12NaN

Creating Pandas Dataframe from Lists

Here is an example of creating dataframe from List of Lists. Let’s look at last 5 days of Facebook stock data.

DateOpenHighLowCloseVolume
02018-08-21172.81174.17171.39172.6219475690
12018-08-20174.04174.57170.91172.5021449120
22018-08-17174.50176.22172.04173.8024819470
32018-08-16180.42180.50174.01174.7031161240
42018-08-15179.34180.87174.78179.5332658240

The other way to create dataframe from list is also using list of tuples.

IndustryShare
0Agriculture & mining10.5
1Manufacturing and construction17.0
2Energy and water supply and waste treatment25.9
3Households22.2
4Services12.4
5Transport sector12.0

Even if you have list of tuples with second element as list, you can use from_items() method.

AmericasAsiaAfricaAustraliaEurope
047.61182035.81394873.46252873.07390593.406278
116.0362549.757809103.50183914.08286753.629875
2119.81933930.65395372.06730657.55954746.241227
334.24940667.76344912.06370180.36698698.966662
428.96392684.52236759.95070680.19062276.823223

Leave a Reply

Your email address will not be published. Required fields are marked *