0% found this document useful (0 votes)
2 views4 pages

Creating DataFrames in Python

This document covers the creation of DataFrame objects in Python using the pandas library. It explains how to create DataFrames from 2-D NumPy arrays and dictionaries, including specifying custom column and index names. Additionally, it demonstrates how to create a DataFrame from another existing DataFrame object.

Uploaded by

volam84413
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Creating DataFrames in Python

This document covers the creation of DataFrame objects in Python using the pandas library. It explains how to create DataFrames from 2-D NumPy arrays and dictionaries, including specifying custom column and index names. Additionally, it demonstrates how to create a DataFrame from another existing DataFrame object.

Uploaded by

volam84413
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class XII- Informatics Practices

Data frames
Module 2 – Creating a DataFrame Object from a 2-D ndarray, Creating a dataFrame object from a
2D dictionary with values as Series Objects, Creating a DataFrame Object from a another DataFrame
Object

Module-2

[Link] a DataFrame Object from a 2-D ndarray –

We can pass a two dimensional NumPy array (i.e. having shape as (<n>,<n>) to DataFrame () to
create a dataframe object.
For Example-1:
import pandas as pd
import numpy as np
n1=[Link]([[1,2,3], [4,5,6]],np.int32)
[Link]
(2,3)
df1=[Link](n1)
df1

Output will be:


0 1 2
0 1 2 3
1 4 5 6

Explanation:
In the above example Python created indexes or axis=0 from the first dimension of the passed
ndarray and columns (axis=1) from the second dimension of the passed ndarray. As no keys are
there so default names are given to indexes and columns.(0 onwards…..)

You can give specify your own column names/index names by giving a column sequence or index
sequence:

For Example-2:
n2= [Link]([[1,2,3], [4,5,6]],np.int32)
[Link]
(2,3)
df2=[Link](n2,columns=[‘one’,’two’,’three’])
df2
Output will be:
one two three
0 1 2 3
1 4 5 6
Explanation:
This time the columns have the names given in columns sequence.

Try this:
Example-3
n3=[Link]([[11.5,21.2,33.8], [40,50,60] , [212.3, 301.5, 405.2]])
df3=[Link](n3,columns=[‘first’,’second’, ‘third’],index=[‘A’,’B’,’C’])
df3
Output:
first second third
A 11.5 21.2 33.8
B 40.0 50.0 60.0
C 212.3 301.5 405.2

Explanation:
This time the columns and indexes have names or labels as per the given columns and index
sequences respectively.

The ndarrays that are passed to DataFrame have same number of elements in each of the rows. If,
however the rows of ndarrays differ in length,means the no. of elements in each row differ ,then
Python will create just single column in the dataframe object and the type of column will be
considered as object.
Example -5
n4=[Link]([[101.5,201.2], [400,50,600,700] , [212.3, 301.5, 405.2]])
n4
Output:
array([list([101.5,201.2]), list([400,50,600,700]) ,list( [212.3, 301.5, 405.2])],dtype=object)

(dataype of the ndarray is object this time)

df4=[Link](n4)
df4
Output: Single column is
0 created because
0 [101.5,201.2] the length of rows
of ndarray did not
1 [400, 50, 600, 700]
match
2 [212.3, 301.5, 405.2]

[Link] a DataFrame object from a 2D dictionary with values as Series Objects:

Create a 2D dictionary having values as three series type objects namely Population, AvgIncome,
and PerCapita.
Example:
Population=[Link]([10927086,12691836, 4631392, 4328063 ],\
index=[ ‘Delhi’,’Patna’,’Mumbai’,’Lucknow’])

AvgIncome= [Link]([72167810927986, 85087812691836, 4226784631392, \


5261784328063],index=[‘Delhi’,’Patna’,’Mumbai’,’Lucknow’])
perCapita=AvgIncome/Population
dct1={0:Population,1:AvgIncome,2:perCapita}
df5=[Link](dct1)
df5

Output:
0 1 2
Delhi 10927086 72167810927986 6.603944e+06
Patna 12691836 85087812691836 6.704137e+06
Munbai 4631392 4226784631392 9.126381e+05
Lucknow 4328063 5261784328063 1.215737e+06

Explanation:
The DataFrame object created has columns assigned from the keys of the dictionary and its index
assigned from the indexes of the series objects which are the values of the dictionary object.

You can change the keys of the dictionary to see the role of the dictionary keys in framing columns
by changing the keys of the above 2D dictionary:
dct2={‘Population’:Population, ’[Link]’ : AvgIncome, ‘Per Capita Income’:percapita}
df6=[Link](dct2)
df6

Output:
[Link] Per Capita Income Population
Delhi 72167810927986 6.603944e+06 10927086
Patna 85087812691836 6.704137e+06 12691836
Munbai 4226784631392 9.126381e+05 4631392
Lucknow 5261784328063 1.215737e+06 4328063

Explanation:
The column names must be valid identifiers of [Link] keys of 2D dictionary have become
columns appear in sorted order.
Indexes have series objects have become indexes.

In the above example Populatation column is not the first column because by default Python
arranges the columns in sorted order hence population column has become the last column.
But we can always specify our own order of columns by specifying the sequence for columns by the
name columns.

df7=[Link](dic2,columns=[‘Population’,’[Link]’,’Per Capita Income’])


df7
Output:

Population [Link] Per Capita Income


Delhi 10927086 72167810927986 6.603944e+06
Patna 12691836 85087812691836 6.704137e+06
Mumbai 4631392 4226784631392 9.126381e+05
Lucknow 4328063 5261784328063 1.215737e+06

4. Creating a DataFrame Object from a another DataFrame Object:


You can create an existing DataFrame object to dataFrame( ) and it will create another DataFrame
object having similar data.
Example:
df8=[Link](df7)
df8
Output:

Population [Link] Per Capita Income


Delhi 10927086 72167810927986 6.603944e+06
Patna 12691836 85087812691836 6.704137e+06
Mumbai 4631392 4226784631392 9.126381e+05
Lucknow 4328063 5261784328063 1.215737e+06

The argument passed (df7) to dataFrame( ) is another dataframe object created earlier.

You might also like