0% found this document useful (0 votes)
8 views2 pages

Pandas - DataFrames Creation

This document provides a practice assignment on creating DataFrames using Pandas in Python. It covers various methods of DataFrame creation including from lists, dictionaries, CSV files, and text files, with examples and code snippets. The document uses color coding for clarity, with code in green, comments in red, and outputs in blue.

Uploaded by

23je0180
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)
8 views2 pages

Pandas - DataFrames Creation

This document provides a practice assignment on creating DataFrames using Pandas in Python. It covers various methods of DataFrame creation including from lists, dictionaries, CSV files, and text files, with examples and code snippets. The document uses color coding for clarity, with code in green, comments in red, and outputs in blue.

Uploaded by

23je0180
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
You are on page 1/ 2

Grade XII Informatics Practices

Chapter: 1 Data Handling using Pandas -II


Practice assignment – Different methods of DataFrame creation
Note: For ease of understanding these notes, the following colour coding is followed:
All code lines: Green. In code wherever >>> is there it is interactive mode, otherwise script mode.
All comments : Red colour
All outputs are given in box in blue colour

1. Creation of Empty dataframe Empty DataFrame


import pandas as pd1 Columns: []
df1 = pd1.DataFrame() Index: []
print(df1)

2. Creation of Dataframe with List 0


import pandas as pd1 0 1
data1 = [1,2,3,4,5] 1 2
df1 = pd1.DataFrame(data1) 2 3
print (df1) 3 4

4 5

3. Dataframe from List with data and column index value


Name Age
import pandas as pd1
0 Freya 10
data1 = [['Freya',10],['Mohak',12],['Dwivedi',13]]
1 Mohak 12
df1 = pd1.DataFrame(data1,columns=['Name','Age'])
2 Dwivedi 13
print (df1)

print(df1['Name']) # retrieving values column wise

print(df1['Age'])

4. Creation of Dataframe with List Data value with float


Name Age
import pandas as pd1
0 Freya 10.0
data1 = [['Freya',10],['Mohak',12],['Dwivedi',13]]
1 Mohak 12.0
df1 = pd1.DataFrame(data1,columns=['Name','Age'],dtype=float)
2 Dwivedi 13.0
print (df1)

# here numeric values dtype changes to float

5. Creation of dataframe from dict and list

import pandas as pd1 Name Age


data1 = {'Name':['Freya', 'Mohak'],'Age':[9,10]} 0 Freya 9
df1 = pd1.DataFrame(data1) 1 Mohak 10
print (df1)
# keys of dictionary becomes the columns of dataframe

6. Creation of a DataFrame from List of Dicts


x y z
import pandas as pd1
0 1 2 NaN
data1 = [ {'x': 1, 'y': 2}, {'x': 5, 'y': 4, 'z': 5}]
1 5 4 5.0
df1 = pd1.DataFrame(data1)

print (df1)

# note that the keys will be taken as column index

# to give row index, index attribute can be used with DataFrame()

# df1 = pd.DataFrame(data1, index=['first', 'second'])

7. Creation of a DataFrame from csv(comma separated value) file / import data from cvs file

# save comma separated values in a csv file in the same folder as python program.

file1.csv

Taken from first row of csv file

import pandas as pd1


Date price factor_1 factor_2
data = pd1.read_csv("file1.csv")
0 2012-06-11 1600.20 1.255 1.548
df1 = pd1.DataFrame(data)
1 2012-06-12 1610.02 1.258 1.554
print (df1)

8. Creating a DataFrame from .txt file

# save some data in a txt file in the same folder as python program.

file2.txt

A B C

import pandas as pd1 0 1 1 12.92

data = pd1.read_table('file2.txt',names=('A', 'B', 'C')) 1 1 2 90.75

df1 = pd1.DataFrame(data) 2 1 3 60.90

print(df1) 3 2 1 71.34

*********************************************

You might also like