Tuesday, September 4, 2018

How To Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorials | Codeing School

This is Pandas Tutorial Part 3. pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School
Slicing, Adding Columns in DataFrame | Pandas Tutorial 3


Today’s Topic: Find How Many Rows and Columns in DataFrame, Slicing in DataFrame, and Adding Columns in DataFrame.


First import pandas and numpy:


import pandas as pd
import numpy as np

Then, take a DataFrame:


df1 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]])
df1


Output:

0
1
2
3
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
3
13
14
15
16
4
17
18
19
20


Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School
Img. no. 2. Take a DataFrame


Find How Many Rows and Columns in DataFrame:


df1.shape

Output:
(5, 4)


Logic: With the help of this df1.shape syntax, we can find out how many rows and columns in our DataFrame. (5, 4), it means we have 5, Rows and 4, Columns.


Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School
Find how many Roe=ws and Columns you have in a DataFrame



Slicing in DataFrame:


Find element in DataFrame:

I want to find ‘10’, at our DataFrame (See, Image 2.).


df1.iloc(2,1)


Output:
10


Logic: We want ‘10’ in the DataFrame. Therefore, I use this syntax df.iloc (2,1). It means (2 is Row and 1 is Column) 3rd Row’s 2nd Column’s value.

Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School
Find element in dataFrame


Access the matrix [10, 11, 14, 15], in DataFrame:
These Values,

Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School


df1.iloc(2:4, 1:3)


Output:

1
2
2
10
11
3
14
15


Logic: We use this df1.iloc(2:4, 1:3) syntax for slicing a matrix in a DataFrame. In this code (2:4, 1:3) means, We start the Row, 2nd number of row to 4th number of Row, because, Slicing logic says, “Starting index to end index + 1”. Here starting index is 2 and end index is 3 + 1.
In the same manner, I take the columns. “1:3” means 1st index to 2nd index + 1. That’s why I get the output, in a matrix format.

Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School
Access Matrix


Adding Columns in DataFrame:
We can add the columns name in Pandas.


df2 = pd.DataFrame([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]], columns=['A','B','C','D'])
df2


Output:

A
B
C
D
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
3
13
14
15
16
4
17
18
19
20


Logic: With the help of this syntax, we can add the column in DataFrame. Here, we have 4 columns, so, “columns = ['A','B','C','D']”.


Slicing, Adding Columns in DataFrame | Pandas Tutorial 3 | Big Data Tutorial | Codeing School
Adding Column 




If you have, any quarry please comment below. 

0 comments:

Post a Comment