CREATION OF DATAFRAME
1. Creation of an empty DataFrame:
Code to create an empty DataFrame is given below
import pandas as pd
DF = [Link]( )
print(DF)
OUTPUT:
Empty DataFrame
Columns: [ ]
Index: [ ]
2. Creation of DataFrame from Lists: We can create dataframe
from list by passing list to DataFrame( ) function. All the
elements of list will be displayed as columns. The default label
of column is 0. for example
Practical 1: To create dataframe from simple list.
import pandas as pd
df = [Link]([11, 22, 33, 44, 55])
print(df)
OUTPUT:
0
0 11
1 22
2 33
3 44
4 55
Practical 2: To create dataframe from simple list by passing
appropriate column heading and row index.
import pandas as pd
df = [Link]([11, 22, 33, 44, 55], index=['R1',
'R2','R3','R4','R5'], columns=['C1'])
print(df)
OUTPUT:
C1
R1 11
R2 22
R3 33
R4 44
R5 55
Practical 3: To create dataframe from nested list.
import pandas as pd
df = [Link]([[21, 'X', 'A'], [32, 'IX', 'B'], [23, 'X', 'A'],
[12, 'XI','A']])
print(df)
OUTPUT:
0 1 2
0 21 X A
1 32 IX B
2 23 X A
3 12 XI A
Practical 4: To create dataframe from nested list by passing
appropriate column heading and row index.
import pandas as pd
df = [Link]([[21, 'X', 'A'], [32, 'IX', 'B'], [23, 'X', 'A'],[12,
'XI','A']], index= ['Rec1', 'Rec2', 'Rec3', 'Rec4'], columns =
["Rno", "Class", "Sec"])
print(df)
OUTPUT:
Rno Class Sec
Rec1 21 X A
Rec2 32 IX B
Rec3 23 X A
Rec4 12 XI A
4. Creation of DataFrame from Dictionary of lists: We can
create dataframe from dictionaries of list as shown below. for
example
Practical 1: To create dataframe using dictionaries of list.
import pandas as pd
df = [Link]({'Rno' : [21, 28, 31], 'Class' : ['IX', 'X', 'XI'],
'Sec' : ['B', 'A','C']})
print(df)
OUTPUT:
Rno Class Sec
0 21 IX B
1 28 X A
2 31 XI C
Practical 2: To create dataframe using dictionaries of list with
appropriate row index.
import pandas as pd
df = [Link]({'B_id' : ['B1', 'B8', 'B5'], 'Sub' : ['Hindi',
'Math', 'Science'], 'Cost' : [450, 520, 400]}, index=['R1', 'R2',
'R3'])
print(df)
OUTPUT:
B_id Sub Cost
R1 B1 Hindi 450
R2 B8 Math 520
R3 B5 Science 400
Note: Dictionary keys become column labels by default in a
DataFrame, and the lists become the rows
5. Creation of DataFrame from List of Dictionaries : We can
create dataframe from list of dictionaries. for example
import pandas as pd
df = [Link]([{'Ram' : 25, 'Anil' : 29, 'Simple' : 28},
{'Ram' : 21, 'Anil' : 25, 'Simple':23}, {'Ram' : 23, 'Anil' : 18,
'Simple' : 26}], index=['Term1', 'Term2', 'Term3'])
print(df)
OUTPUT:
Ram Anil Simple
Term1 25 29 28
Term2 21 25 23
Term3 23 18 26
NOTE: NaN (Not a Number) is inserted if a corresponding value
for a column is missing as shown in the following example.
import pandas as pd
df = [Link]([{'Ram' : 25, 'Anil' : 29, 'Simple' : 28},
{'Ram' : 21, 'Anil' : 25, 'Simple':23}, {'Ram' : 23, 'Anil' : 18}],
index=['Term1', 'Term2', 'Term3'])
print(df)
OUTPUT:
Ram Anil Simple
Term1 25 29 28
Term2 21 25 23
Term3 23 18 NaN
6. Creation of DataFrame from Series : We can create
dataframe from single or multiple Series. for example
Example 1: Creation of DataFrame from Single Series.
import pandas as pd
S1 = [Link]([10, 20, 30, 40])
S2 = [Link]([11, 22, 33, 44])
S3 = [Link]([34, 44, 54, 24])
df = [Link](S1)
print(df)
OUTPUT:
0
0 10
1 20
2 30
3 40
Example 3: Creation of DataFrame from three Series.
import pandas as pd
S1 = [Link]([10, 20, 30, 40])
S2 = [Link]([11, 22, 33, 44])
S3 = [Link]([34, 44, 54, 24])
df = [Link]([S1, S2, S3],index = ['R1', 'R2', 'R3'])
print(df)
OUTPUT:
0 1 2 3
R1 10 20 30 40
R2 11 22 33 44
R3 34 44 54 24
To create a DataFrame using more than one series, we need to
pass multiple series in the list as shown above
Attributes of DataFrames
Like Series, we can access certain properties called attributes
of a DataFrame. Some Attributes of Pandas DataFrame are
1. [Link]: This attribute display all the row labels of
dataframe.
2. [Link]: This attribute display all the column
labels of the dataframe.
3. [Link]: This attribute display data type of each
column in the dataframe.
4. [Link]: This attribute display a tuple
representing the dimensions of the dataframe. In other words
it simply displays the number of rows and columns in the
dataframe.
5. [Link]: This attribute simply displays total number
of values in the dataframe.
6. DataFrame.T: This attribute transpose the DataFrame.
Means, row indices and column labels of the DataFrame
replace each other’s position.
7. [Link]: This attribute display a NumPy ndarray
having all the values in the DataFrame, without the axes
labels.
8. [Link]: This attribute returns the value True if
DataFrame is empty and False otherwise.
import pandas as pd
df = [Link]([[25, 29, 28, 17], [21, 25, 23, 20], [23, 18,
26, 23],[20, 18, 30, 15]], index=['R1', 'R2', 'R3', 'R4'], columns
= ['Ram', 'Anil', 'Simple', 'Anuj'])
print(df)
print("---------------------------------------------------")
print([Link])
print("---------------------------------------------------")
print([Link])
print("---------------------------------------------------")
print([Link])
print("---------------------------------------------------")
print([Link])
print("---------------------------------------------------")
print([Link])
print("---------------------------------------------------")
print(df.T)
print("---------------------------------------------------")
print([Link])
print("---------------------------------------------------")
print([Link])
OUTPUT:
DATAFRAME
Ram Anil Simple Anuj
R1 25 29 28 17
R2 21 25 23 20
R3 23 18 26 23
R4 20 18 30 15
INDEX
Index(['R1', 'R2', 'R3', 'R4'], dtype='object')
ROWS LABEL
Index(['Ram', 'Anil', 'Simple', 'Anuj'], dtype='object')
DTYPES
Ram int64
Anil int64
Simple int64
Anuj int64
dtype: object
SHAPE
(4, 4)
---------------------------------------------------
SIZE
16
TRANSPOSE
R1 R2 R3 R4
Ram 25 21 23 20
Anil 29 25 18 18
Simple 28 23 26 30
Anuj 17 20 23 15
VALUES
[[25 29 28 17]
[21 25 23 20]
[23 18 26 23]
[20 18 30 15]]
EMPTY
False