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

Pandas - Dataframe - Introduction

Pandas dataframes in python Pandas are important For becoming a data analyst,data scientist That's it

Uploaded by

madhus.naragani
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 views16 pages

Pandas - Dataframe - Introduction

Pandas dataframes in python Pandas are important For becoming a data analyst,data scientist That's it

Uploaded by

madhus.naragani
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/ 16

Data Science - Pandas - DataFrame Introduction

Contents
1. DataFrame .......................................................................................................................................... 2
1.1. DataFrame is a pre-defined class ................................................................................................. 2
2. Create DataFrame .............................................................................................................................. 4
2.1. Create an Empty DataFrame ........................................................................................................ 5
2.2. Create a DataFrame by using list ................................................................................................. 6
2.3. Creating a DataFrame by using list of lists ................................................................................... 9
2.4. Creating a DataFrame by using dictionary ................................................................................. 12
2.5. Creating DataFrame by loading the files. ................................................................................... 16

1|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

6. PANDAS – DATAFREAM – INTRODUCTION

1. DataFrame

 A Data frame is a two-dimensional data structure.


 Data frame is just like a table.
 Data frame contains rows and columns.

1.1. DataFrame is a pre-defined class

 DataFrame is a pre-defined class in pandas library.

Example

Emp_No Name Salary

101 Ranjan 10000

102 Akshay 20000

103 Daniel 30000

104 Veeru 40000

2|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

Columns and Rows are

 Columns are

o First column name is : Emp_No


o Second column name is : Name
o Third column name is : Salary

 Rows are

o First row data is : 101 Abhi 10000


o Second row data is : 102 Akshay 20000
o Third row data is : 103 Daniel 10000
o Forth row data is : 104 Veeru 10000

3|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2. Create DataFrame

 DataFrame is a predefined class in pandas.


 We can create DataFrame in different ways like below,
o Empty DataFrame
o By using single list
o By using nested list
o By using dictionary
o with another DataFrame
o Loading files(real time approach)

Generally

 In real time when we load existing file then it returns DataFrame

4|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.1. Create an Empty DataFrame

 We can create an empty DataFrame

Program Creating empty DataFrame


Name demo1.py

import pandas as pd

df = pd.DataFrame()
print(df)
print(type(df))

Output

Empty DataFrame
Columns: []
Index: []

<class 'pandas.core.frame.DataFrame'>

5|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.2. Create a DataFrame by using list

 We can create DataFrame by using single list.


o If we are using single list then it’s a single column DataFrame
o If we are using list of lists(nested lists) then it’s multiple columns
DataFrame

Program Creating DataFrame by using single list


Name demo2.py

import pandas as pd

a = [10, 20, 30, 40, 50, 60, 70, 80, 90]


df = pd.DataFrame(a)

print(df)
print(type(df))

Output

Note

 From the output, DataFrame created with single column.


 Here column name is Zero, we can customise this as well.

6|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

Note on index

 If no index is passed, then by default, index will be range(n), where n is


the array length

Program Creating DataFrame by using single list


Name demo3.py

import pandas as pd

names = ["Ranjan", "Sagar", "Daniel", "Prasad", "Kumari",


"Pravallika", "Arjun", "Akshay"]
df = pd.DataFrame(names)

print(df)

Output

7|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

Program Creating single column DataFrame and checking length


Name demo4.py

import pandas as pd

names = ["Ranjan", "Sagar", "Daniel", "Prasad", "Kumari",


"Pravallika", "Arjun", "Akshay"]
df = pd.DataFrame(names)

print(df)
print()
print("The length is:", len(df))

Output

8|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.3. Creating a DataFrame by using list of lists

 We can create DataFrame with list of lists (nested list).


 If we are using list of lists then it create a DataFrame with multiple
columns.

9|Page 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

Program Creating DataFrame by using list of lists


Name demo5.py

import pandas as pd

details = [
["Ranjan", 11],
["Sagar", 12],
["Daniel", 13],
["Prasad", 14],
["Kumari", 15],
["Pravallika", 16],
["Arjun", 17],
["Akshay", 18]
]

df = pd.DataFrame(details)

print(df)

Output

Note

 From the output, DataFrame created with two columns.


 Here column names are 0 and 1 and we can customise this as well.

10 | P a g e 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.3.1. Giving column names to DataFrame


 We can give column names to DataFrame.

Program Creating DataFrame and giving names to columns


Name demo6.py

import pandas as pd

details = [
["Sagar", 20, 10000],
["Daniel", 16, 20000],
["Veeru", 24, 30000],
["Raju", 25, 40000],
["Kiran", 26, 50000],
["Kedar", 27, 60000],
["Reena", 28, 70000],
["Karthik", 29, 80000],
["Satish", 30, 90000]
]

cols = ["Name", "Age", "Salary"]

df = pd.DataFrame(details, columns = cols)

print(df)

Output

11 | P a g e 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.4. Creating a DataFrame by using dictionary

 We can create DataFrame by using dictionary


 If we are using list of lists then it create a DataFrame with multiple
columns.

Program Creating DataFrame by using dictionary


Name demo8.py

import pandas as pd

details = {
"Name": ["Daniel", "Abhi", "Veeru", "Raju", "Kiran",
"Kedar", "Reena", "Karthik", "Satish"],

"Age": [20, 21, 23, 24, 25, 26, 27, 28, 29]
}

df = pd.DataFrame(details)

print(df)

Output

Note

 In above example Name and age considered as column names

12 | P a g e 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.4.1. We can customize the index values


 By default index value start from 0
 We can customise the index values in DataFrame.
 If index is passed, then the length of the index should equal to the length
of the DataFrame.

13 | P a g e 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

Program Creating DataFrame and giving index


Name demo9.py

import pandas as pd

details = [
["Sagar", 20, 10000],
["Daniel", 16, 20000],
["Veeru", 24, 30000],
["Raju", 25, 40000],
["Kiran", 26, 50000],
["Kedar", 27, 60000],
["Reena", 28, 70000],
["Karthik", 29, 80000],
["Satish", 30, 90000]
]

c = ["Name", "Age", "Salary"]


i = [11, 22, 33, 44, 55, 66, 77, 88, 99]

df = pd.DataFrame(details, columns = c, index = i)

print(df)

Output

14 | P a g e 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

Program Creating DataFrame and giving index


Name demo10.py

import pandas as pd

details = [
["Sagar", 20, 10000],
["Daniel", 16, 20000],
["Veeru", 24, 30000],
["Raju", 25, 40000],
["Kiran", 26, 50000],
["Kedar", 27, 60000],
["Reena", 28, 70000],
["Karthik", 29, 80000],
["Satish", 30, 90000]
]

c = ["Name", "Age", "Salary"]


i = ["Row1", "Row2", "Row4", "Row5", "Row6", "Row7", "Row8",
"Row9", "Row10"]

df = pd.DataFrame(details, columns = c, index = i)

print(df)

Output

15 | P a g e 6.PANDAS – DATAFRAME - INTRO


Data Science - Pandas - DataFrame Introduction

2.5. Creating DataFrame by loading the files.

 We can create DataFrame by loading files like csv, json etc.


 This we will learn more on 8th chapter.

16 | P a g e 6.PANDAS – DATAFRAME - INTRO

You might also like