Convert a table to dataframe in python

I have a dictionary[array]. I need his appearance to be the same as in the photo. How to do it?

Photo with a table that should be obtained at the output

My Code

array = {1: {2016: 22818888925.021294, 2017: 23215791927.55883, 2018: 24241415053.75489, 2019: 25526831987.89783, 2007: 18183059018.027966, 2008: 16868527277.51174, 2009: 18331809529.53519, 2010: 19724006864.797657, 2011: 20392912788.825798, 2012: 19580849153.840134, 2013: 22084393933.539394, 2014: 22030084567.47173, 2015: 21700822555.267574}, 2: {2016: 15081873888.10038, 2017: 15377071873.462284, 2018: 16253879799.12362, 2019: 16517025291.019705, 2007: 10038529774.615318, 2008: 11611749842.042978, 2009: 12040185617.510305, 2010: 12508816879.258118, 2011: 13711111085.100977, 2012: 13219008951.675236, 2013: 14209627046.021126, 2014: 15371996764.61913, 2015: 14417938720.997215}, 6: {2016: 0.6609381349660216, 2017: 0.6623539667069717, 2018: 0.6705004539991144, 2019: 0.6470456380505956, 2007: 0.5520814602571774, 2008: 0.6883677306864346, 2009: 0.6567919876164886, 2010: 0.6341924825418297, 2011: 0.6723468700662476, 2012: 0.6750988605150847, 2013: 0.6434239077958611, 2014: 0.6977729348945159, 2015: 0.6643959547744175}}]

print pd.DataFrame.from_dict[mass_factor] 

Output of my code

           1             2         6
2007  1.818306e+10  1.003853e+10  0.552081
2008  1.686853e+10  1.161175e+10  0.688368
2009  1.833181e+10  1.204019e+10  0.656792
2010  1.972401e+10  1.250882e+10  0.634192
2011  2.039291e+10  1.371111e+10  0.672347
2012  1.958085e+10  1.321901e+10  0.675099
2013  2.208439e+10  1.420963e+10  0.643424
2014  2.203008e+10  1.537200e+10  0.697773
2015  2.170082e+10  1.441794e+10  0.664396
2016  2.281889e+10  1.508187e+10  0.660938
2017  2.321579e+10  1.537707e+10  0.662354
2018  2.424142e+10  1.625388e+10  0.670500
2019  2.552683e+10  1.651703e+10  0.647046

At times, you may need to convert a list to Pandas DataFrame in Python.

You may then use this template to convert your list to a DataFrame:

import pandas as pd
list_name = ['item_1', 'item_2', 'item_3',...]
df = pd.DataFrame [list_name, columns = ['column_name']]

In the next section, you’ll see how to perform the conversion in practice.

Examples of Converting a List to Pandas DataFrame

Example 1: Convert a List

Let’s say that you have the following list that contains 5 products:

products_list = ['laptop', 'printer', 'tablet', 'desk', 'chair']

You can then apply the following syntax in order to convert the list of products to Pandas DataFrame:

import pandas as pd

products_list = ['laptop', 'printer', 'tablet', 'desk', 'chair']

df = pd.DataFrame [products_list, columns = ['product_name']]
print [df]

This is the DataFrame that you’ll get:

  product_name
0       laptop
1      printer
2       tablet
3         desk
4        chair

Example 2: Convert a List of Lists

How would you then convert a list of lists to a DataFrame?

For instance, let’s say that you have the following list of lists:

products_list = [['laptop',1300],['printer',150],['tablet',300],['desk',450],['chair',200]]

You can then run the code below to perform the conversion to a DataFrame:

import pandas as pd

products_list = [['laptop',1300],['printer',150],['tablet',300],['desk',450],['chair',200]]

df = pd.DataFrame [products_list, columns = ['product_name', 'price']]
print [df]

And this is the result that you’ll get:

  product_name  price
0       laptop   1300
1      printer    150
2       tablet    300
3         desk    450
4        chair    200

Alternatively, you may have your list of lists as follows:

products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]]

Therefore, the Python code to perform the conversion to a DataFrame would be:

import pandas as pd

products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]]

df = pd.DataFrame [products_list].transpose[]
df.columns = ['product_name', 'price']
print [df]

Run the code, and you’ll get the same DataFrame:

  product_name  price
0       laptop   1300
1      printer    150
2       tablet    300
3         desk    450
4        chair    200

Check the Object Type

If needed, you may also check the type of the objects [e.g., List vs. DataFrame] by applying this code:

import pandas as pd

products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]]

df = pd.DataFrame [products_list].transpose[]
df.columns = ['product_name', 'price']

print ['products_list: ' + str[type[products_list]]]
print ['df: ' + str[type[df]]]

And here is the result:

products_list: 
df: 

Applying Stats Using Pandas [optional]

Once you converted your list into a DataFrame, you’ll be able to perform an assortment of operations and calculations using Pandas.

For instance, you may use Pandas to derive some statistics about your data.

In the context of our example, you can apply the code below in order to get the mean, max and min price using Pandas:

import pandas as pd

products_list = [['laptop', 'printer', 'tablet', 'desk', 'chair'],[1300, 150, 300, 450, 200]]

df = pd.DataFrame [products_list].transpose[]
df.columns = ['product_name', 'price']

mean_value = df['price'].mean[]
max_value = df['price'].max[]
min_value = df['price'].min[]

print ['The mean price is: ' + str[mean_value]]
print ['The max price is: ' + str[max_value]]
print ['The min price is: ' + str[min_value]]

Run the Python code, and you’ll get these stats:

The mean price is: 480
The max price is: 1300
The min price is: 150

An Opposite Scenario

Sometimes, you may face an opposite situation, where you’ll need to convert a DataFrame to a list. If that’s the case, you may want to check the following guide that explains the steps to perform the conversion.

How do you load a table into a DataFrame?

This recipe explains how to load the table from MySQL database and then converts it into the dataframe using Pyspark..
System requirements :.
Step 1: Import the modules..
Step 2: Connect to Mysql and load the table..
Step 3: Print the Schema of the Table..
Step 4: To View Data of the Table..
Conclusion..

How do you create a DataFrame in python?

Method - 3: Create Dataframe from dict of ndarray/lists.
import pandas as pd..
# assign data of lists..
data = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18]}.
# Create DataFrame..
df = pd.DataFrame[data].
# Print the output..
print[df].

How do you read a table in python?

Key Points.
Use the Pandas library to get basic statistics out of tabular data..
Use index_col to specify that a column's values should be used as row headings..
Use DataFrame.info to find out more about a dataframe..
The DataFrame. ... .
Use DataFrame. ... .
Use DataFrame..

Can we convert list into DataFrame in python?

For Converting a List into Pandas Core Data Frame, we need to use DataFrame Method from pandas Package.

Chủ Đề