- Get link
- X
- Other Apps
In [2]:
# from a Python lis
# Import the library - in this case pandas
import pandas as pd
# create and initialize a list of multiple lists
multilist = [['george', 2], ['peter', 25], ['alice', 24]]
# Create DataFrame
df = pd.DataFrame(multilist, columns = ['First Name', 'Age'])
# print (show) dataframe.
df
Out[2]:
First Name | Age | |
---|---|---|
0 | george | 2 |
1 | peter | 25 |
2 | alice | 24 |
In [3]:
# Method #2: Creating DataFrame from dict of an narray/lists
# To create DataFrame from a python dictionary of narray/list, all the narray must be of same length.
# If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will
# be range(n) where n is the array length.
In [3]:
# DataFrame from dict narray / lists
# Addresses by default
import pandas as pd
# intialise data of lists.
data = {'Last Name':['George', 'Peter', 'Alice', 'John'],
'Age':[33, 54, 9, 82],
'School':['VT', 'NYC', 'UCLA', 'GM']}
# Create DataFrame
df = pd.DataFrame(data)
# Print the output.
df
Out[3]:
Last Name | Age | School | |
---|---|---|---|
0 | George | 33 | VT |
1 | Peter | 54 | NYC |
2 | Alice | 9 | UCLA |
3 | John | 82 | GM |
In [6]:
Here we create an indexes DataFrame using arrays.
In [8]:
# pandas DataFrame with indexed by DataFrame using arrays.
import pandas as pd
# initialise data of lists.
data = {' Last Name':['George', ')Peter', 'Alice', 'John'],
'Points':[39, 55,22, 95],
'School':['VT', 'NYC','UCLA','GM']}
# Creates pandas DataFrame.
df = pd.DataFrame(data, index =['Position1',
'Position2',
'Position3',
'Position4'])
# print (show) the data
df
Out[8]:
Name | marks | School | |
---|---|---|---|
rank1 | Tom | 99 | VT |
rank2 | Jack | 98 | NYC |
rank3 | nick | 95 | NYC |
rank4 | juli | 90 | UCLA |
In [9]:
# Create a Dataframe from list of dictionaries (multiple dictionaries)
# Pandas DataFrame can be created by passing lists of dictionaries as a input data.
# By default dictionary keys are consider the columns columns and the values are the rows.
In [4]:
# Pandas DataFrame by lists of dictionaries.
import pandas as pd
# Initialise data to lists.
data = [{'a': 10, 'b': 100, 'c':300},
{'a':20, 'b': 200, 'c': 400}]
# Creates DataFrame.
df = pd.DataFrame(data)
# Print (show) the data
df
Out[4]:
a | b | c | |
---|---|---|---|
0 | 10 | 100 | 300 |
1 | 20 | 200 | 400 |
In [11]:
# Let's generate pandas DataFrame by passing lists of dictionaries and row indexes.
In [7]:
# Pandas DataFrame by passing lists of Dictionaries and row indices.
import pandas as pd
# Intitialise data of lists
dataone = [{'b':6, 'c':8}, {'a':100, 'b':200, 'c':300}]
# Creates padas DataFrame by passing Lists of dictionaries and row index.
df = pd.DataFrame(dataone, index = ['first', 'second'])
# Print (show) the data
df
In [13]:
# Another example is to create pandas DataFrame from lists of dictionaries with both row index as well as
# column index.
In [8]:
# Pandas DataFrame with lists of dictionaries as well as row and column indexes.
import pandas as pd
# Intitialise lists data.
data = [{'a': 1, 'b': 2},
{'a': 5, 'b': 10, 'c': 20}]
# With two column indices, values same as dictionary keys
df1 = pd.DataFrame(data, index =['first',
'second'],
columns =['a', 'b'])
# With two column indices with one index with other name
df2 = pd.DataFrame(data, index =['first',
'second'],
columns =['a', 'b1'])
# print (show) first data frame
print (df1, "\n")
# Print (show) second DataFrame.
print (df2)
a b first 1 2 second 5 10 a b1 first 1 NaN second 5 NaN
In [15]:
# DataFrame using zip() function.
# Two lists can be merged by using list(zip()) function. Now, create the pandas DataFrame by calling
# pd.DataFrame() function.
In [16]:
# pandas Datadaframe from lists using zip.
import pandas as pd
# List1
Name = ['tom', 'krish', 'nick', 'juli']
# List2
Age = [25, 30, 26, 22]
# get the list of tuples from two lists.
# and merge them by using zip().
list_of_tuples = list(zip(Name, Age))
# Assign data to tuples.
list_of_tuples
# Converting lists of tuples into
# pandas Dataframe.
df = pd.DataFrame(list_of_tuples,
columns = ['Name', 'Age'])
# Print data.
df
Out[16]:
Name | Age | |
---|---|---|
0 | tom | 25 |
1 | krish | 30 |
2 | nick | 26 |
3 | juli | 22 |
In [17]:
# Pandas DataFrame from Dictionaries of series.
# To create DataFrame from Dicts of series, dictionary can be passed to form a DataFrame. The resultant
# index is the union of all the series of passed indexed.
In [18]:
# Pandas Dataframe from Dicts of series.
import pandas as pd
# Intialise data to Dicts of series.
d = {'one' : pd.Series([10, 20, 30, 40],
index =['a', 'b', 'c', 'd']),
'two' : pd.Series([10, 20, 30, 40],
index =['a', 'b', 'c', 'd'])}
# creates Dataframe.
df = pd.DataFrame(d)
# print the data.
df
Out[18]:
one | two | |
---|---|---|
a | 10 | 10 |
b | 20 | 20 |
c | 30 | 30 |
d | 40 | 40 |
- Get link
- X
- Other Apps
Comments
Post a Comment