Importing data into pandas from Python data structures

The first step in working with pandas DataFrames is to create one using the pandas constructor function, DataFrame(). The constructor takes many Python data structures as input. It also takes as input NumPy arrays and pandas Series, another type of one-dimensional pandas data structure that is similar to a list. Here we demonstrate how to convert a dictionary of lists into a DataFrame:

import pandas as pd
data = {
'col1': [1, 2, 3],
'col2': [4, 5, 6],
'col3': ['x', 'y', 'z']
}

df = pd.DataFrame(data)
print(df)

The output is as follows:

   col1  col2 col3
0     1     4    x
1     2     5    y
2     3     6    z

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset