0% found this document useful (0 votes)
42 views

3a Data Frame - Jupyter Notebook

The document discusses Pandas DataFrames, including how to create DataFrames from lists, dictionaries, and tables. It demonstrates selecting, deleting, and adding columns and rows. Methods for viewing subsets of data like head and tail are presented. Finally, it shows how to merge DataFrames on indexes or column values.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

3a Data Frame - Jupyter Notebook

The document discusses Pandas DataFrames, including how to create DataFrames from lists, dictionaries, and tables. It demonstrates selecting, deleting, and adding columns and rows. Methods for viewing subsets of data like head and tail are presented. Finally, it shows how to merge DataFrames on indexes or column values.

Uploaded by

venkatesh m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1/20/22, 9:59 PM 3a Data Frame - Jupyter Notebook

In [1]: # Pandas DataFrame

In [2]: # import packages


import pandas as pd
import numpy as np

test =[1,2,3,4,5]
test

Out[2]: [1, 2, 3, 4, 5]

In [2]: df1 = pd.Series(test)


df1

...

In [3]: df = pd.DataFrame(test)
df

...

In [3]: ## Creating a Table


table = [['srinu',25],['kishore',26],['mani',27],['ratna',28]]

table

...

In [4]: df = pd.DataFrame(table,columns =['Name','Age'])



df

...

In [3]: table1 = {'Name':['srinu','kishore','mani','ratna'],'Age':[25,26,27,28]}



df = pd.DataFrame(table1,index=[1,2,3,4])

df


...

localhost:8890/notebooks/3 Numpy and Pandas/3a Data Frame.ipynb 1/5


1/20/22, 9:59 PM 3a Data Frame - Jupyter Notebook

In [4]: ## Columns Selection , deletion , addition


df['weight'] = pd.Series([65,75,85,90],index = [1,2,3,4])

df

...

In [5]: df['height'] = pd.Series([150,151,152,153],index = [1,2,3,4])


df

...

In [3]: # creating the table


table12 = {'Name':['srinu','kishore','mani','ratna','ramu'],'Age':[25,26,27,28,29
table12
td = pd.DataFrame(table12,index=[1,2,3,4,5])
td

Out[3]: Name Age height weight

1 srinu 25 151 65

2 kishore 26 152 76

3 mani 27 153 78

4 ratna 28 154 87

5 ramu 29 155 88

In [4]: # deleting the column


del td['Age']
td
# other method to delete the column
#td.pop('two')

Out[4]: Name height weight

1 srinu 151 65

2 kishore 152 76

3 mani 153 78

4 ratna 154 87

5 ramu 155 88

In [5]: ​
td

...

localhost:8890/notebooks/3 Numpy and Pandas/3a Data Frame.ipynb 2/5


1/20/22, 9:59 PM 3a Data Frame - Jupyter Notebook

In [6]: # row selection


td.iloc[3:5]
# giving the index number to select the particular Row

...

In [13]: # multiple Selection


td[1:5]

...

In [9]: # Addition of Rows to existing


df = pd.DataFrame([[1,2],[3,4]],columns=['a','b'])

In [10]: df

...

In [11]: df2 = pd.DataFrame([[5,6],[7,8]],columns=['a','b'])


df2

...

In [12]: ## To add rows


df = df.append(df2)
df

...

In [13]: ​
df = pd.DataFrame([[1,2],[3,4]],columns=['a','b'])
df2 = pd.DataFrame([[5,6],[7,8]],columns=['a','b'])
df = df.append(df2)
df

...

In [19]: td
td.head(6)

Out[19]: Name height weight

1 srinu 151 65

2 kishore 152 76

3 mani 153 78

4 ratna 154 87

5 ramu 155 88

In [23]: td.tail(3)

...

localhost:8890/notebooks/3 Numpy and Pandas/3a Data Frame.ipynb 3/5


1/20/22, 9:59 PM 3a Data Frame - Jupyter Notebook

In [1]: import pandas as pd

In [14]: df1=pd.DataFrame(
{"emp_id":[1,2,5,4],'Name': ["Alex","John","Abhishek","Watson"], 'Age': [25,3

In [15]: df2=pd.DataFrame(
{"emp_id":[1,2,3,4],'Name': ["Alex","John","Krishna","Abhishek"], 'Weight': [

In [4]: df1

Out[4]: emp_id Name Age

0 1 Alex 25

1 2 John 30

2 5 Abhishek 24

3 4 Watson 20

In [5]: df2

Out[5]: emp_id Name Weight

0 1 Alex 60

1 2 John 65

2 3 Krishna 70

3 4 Abhishek 75

In [6]: pd.merge(df1,df2)

Out[6]: emp_id Name Age Weight

0 1 Alex 25 60

1 2 John 30 65

In [7]: pd.merge(df1,df2,how="outer")

Out[7]: emp_id Name Age Weight

0 1 Alex 25.0 60.0

1 2 John 30.0 65.0

2 5 Abhishek 24.0 NaN

3 4 Watson 20.0 NaN

4 3 Krishna NaN 70.0

5 4 Abhishek NaN 75.0

localhost:8890/notebooks/3 Numpy and Pandas/3a Data Frame.ipynb 4/5


1/20/22, 9:59 PM 3a Data Frame - Jupyter Notebook

In [8]: pd.merge(df1,df2,how="outer",on=["Name"])

# it will add the based on the name columns

Out[8]: emp_id_x Name Age emp_id_y Weight

0 1.0 Alex 25.0 1.0 60.0

1 2.0 John 30.0 2.0 65.0

2 5.0 Abhishek 24.0 4.0 75.0

3 4.0 Watson 20.0 NaN NaN

4 NaN Krishna NaN 3.0 70.0

In [ ]: ​

localhost:8890/notebooks/3 Numpy and Pandas/3a Data Frame.ipynb 5/5

You might also like