Professional Documents
Culture Documents
DataFrame Creation
DataFrame Creation
DataFrame Creation
2
DATA FRAME
3
DATA FRAME CREATION
SYNTAX
pandas.DataFrame(data, index, columns)
data: It is a dataset from which dataframe is to be created. It can be list,
dictionary, scalar value, series, ndarrays, etc.
index: It is optional, by default the index of the dataframe starts from 0
and ends at the last data value(n-1). It defines the row label explicitly.
columns: This parameter is used to provide column names in the
dataframe. If the column name is not defined by default, it will take a
value from 0 to (n-1)
4
CREATING EMPTY DATA FRAME
print(df)
5
DATA FRAME CREATION METHODS
I. List of Lists
II. List of Dictionary
III.List of Array
IV. Nested Array
V. List of Series
VI. Dict of Lists
VII.Dict of Dictionary
VIII.Dict of Array
IX.Dict of Series
6
CREATION OF DATA FRAME BY LIST OF LISTS
import pandas as pd
print (df)
OUTPUT
7
CREATION OF DATA FRAME BY LIST OF DICTIONARY
import pandas as pd
OUTPUT
8
CREATION OF DATAFRAME BY LIST OF ARRAYS
import pandas as pd OUTPUT
import numpy as np
a=np.array([‘Jai’,’Msc’])
b=np.array( [‘Princi’,’MA’])
c=np.array([‘Gaurav’,’MCA’])
d=np.array [‘Anuj’,’Phd’])
e=[a,b,c,d]
df=pd.DataFrame(e,columns=[‘Name’,’Qualification’])
print(df)
9
CREATION OF DATA FRAME BY NESTED ARRAY
row= [1, 2, 3, 4]
df = pd.DataFrame(array,index=row,columns=column)
print(df)
10
CREATION OF DATAFRAME BY LIST OF SERIES
import pandas as pd
df = pd.DataFrame([A,B],index=['one','two'])
print(df)
OUTPUT
11
CREATION OF DATA FRAME BY DICT OF LIST
import pandas as pd
data = {'Name': ['Tom', 'Jack', 'nick', 'juli'],'marks': [99, 98, 95, 90]}
print(df)
OUTPUT
12
CREATION OF DATA FRAME BY DICT OF DICTIONARY
import pandas as pd
dp = pd.DataFrame(dict,index=idx)
OUTPUT
13
CREATION OF DATA FRAME BY DICT OF ARRAY
df = pd.DataFrame(data)
print(df)
14
CREATION OF DATA FRAME BY DICT OF SERIES
import pandas as pd OUTPUT
a=pd.Series([‘Ankit’,’Golu’,’Sanjay’]) Rollno Name
b=pd.Series([21,10,55]) 0 21 Ankit
df=pd.DataFrame(c) 2 55 Sanjay
print(df)
15
16