0% found this document useful (0 votes)
12 views13 pages

Working With Panda

The document discusses various features of pandas DataFrame such as creating, displaying, modifying, filtering and analyzing DataFrames. It includes 30 examples demonstrating operations like creating DataFrames from different data types, accessing and modifying values, adding and deleting columns, filtering rows, sorting, describing and analyzing DataFrames.

Uploaded by

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

Working With Panda

The document discusses various features of pandas DataFrame such as creating, displaying, modifying, filtering and analyzing DataFrames. It includes 30 examples demonstrating operations like creating DataFrames from different data types, accessing and modifying values, adding and deleting columns, filtering rows, sorting, describing and analyzing DataFrames.

Uploaded by

Sheryl Viniba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Ex.

No : 05
Date:
WORKING WITH PANDAS DATA FRAMES

Aim
To gain knowledge in various features of pandas DataFrame.

Pandas DataFrame
The pandas data frame is a structure that contains two dimensional data and that
contains two dimensional data and its corresponding labels . Data frames are widely used in data
science , machine learning , scientific computing and many other data intensive fields.

Importing pandas
>>> import pandas as pd

1. Create pandas Dataframe

Code
import pandas as pd data = {
'Name':['A','B','C','D','E','F'],
'Reg No':[7111,7112,7113,7114,7115,7116], 'Age':[16,17,17,16,17,16],
'Mail Id': ['[email protected]','[email protected]','[email protected]','[email protected]',
'[email protected]','F@ gmail.com'],
'State':['AP','TN','KA','AP','TS','KL'],
'Mother Tongue':['Telugu','Tamil','Kannada','Telugu','Telugu','Malayalam']
}
df = pd.DataFrame(data = data)

2. Displaying the DataFrame

Code

>>> df
Output

3. Display first 3 rows ad last 2 rows in the Dataframe

Code
For first 3 rows
>>> df.head(n =3)

Output

Code
For last 2 rows
>>> df.tail(n = 2)

Output
4. Displaying any one column

Code
>>> State = df[‘State’] df.State
Output

5. Displaying the value corresponding to label

Code
>>> df.loc[4]

Output

6. Creating pandas data frame with dictionaries

Code
>>> data = {'Reg No':[7111,7112,7113],'Age': np.array([15,16,15]),'State':"AP"}

pd.DataFrame(data)

Output

7. Creating a pandas DataFrame with lists


Code
>>>data = [{'x':1,'y':2,'z':100},

{'x':2,'y':3,'z':100},

{'x':3,'y':4,'z':100}]

pd.DataFrame(data)

Output

8. Creating a pandas Dataframe with Numpy arrays

Code
>>> c = np.array([[1,2,100],

[2,3,100],

[3,4,100]])

df= pd.DataFrame(c, columns = ['x','y','z'])

>>> df

Output

9. Creating a pandas Dataframe from files

Code
>>>c = pd.read_csv('student.csv') c

Output
10. Displaying data frame row labels and column labels

For Row labels


Code
>>> df.index

Output

For Column labels


Code
>>> df.columns

Output

11. Modifying the labels

Code

>>>import numpy as np df.index = np.arange(10,16) df.index

Output

12. Convert DataFrame into numpy array

Code
>>> df.to_numpy()

Output
13. Displaying datatypes for each column to a pandas data frame

Code
>>> df.dtypes

Output

14. Displaying the number of dimensions, number of data values across each
dimension and total number of data values in Dataframe.

Code:
>>> df.ndim
Output: 2

Code
>>> df.shape
Output: (6,6)

Code
>>> df.size
Output: 36

15. Checking the amount of memory used by each column

Code
>>> df.memory_usage()

Output
16. Displaying elements in particular row label Code:

Code
>>> df[‘Name']

Output

17. Retrieving a row or column by its integer index Code:

Code
>>> df.loc[10]

Output

18. Retrieving single value , using row label pandas recommends using specialised
accessors at [] and iat[]

Code
>>> df.at[10,’Name']
Output: ‘A’

Code
>>> df.iat[0,1]
Output: 7111

19. Changing the age of three students we ca use accessors to modify part of a pandas
data frame by passing a python sequence Numpy array or single values.

Code
>>>df.loc[:12,'Age']=[24,26,28]
df[‘Age']
Output

20. Adding the details of new student in the existing pandas Dataframe

Code
>>> G = pd.Series(data=['G',7117,17,'[email protected]','KA','Kannada'],
index = df.columns,name = 16)
df = df.append(G) df

Output

21. Deleting any one row in data frame .

Code
>>> df = df.drop(labels =[14]) df
Output
22. Adding a column CGPA in the data frame at the last

Code
>>> df['CGPA']= np.array([6.5,8.3,7.9,9.6,8.1,9.3])
df

Output

23. Adding a column DOB before age in the date frame

Code
>>> df.insert(loc=3,column = 'DOB',value = np.array(['5/10/2004',
'3/10/2005','10/10/2005','3/1/2005','14/10/2005','20/06/200 4']))

df

Output

24. Deleting a column DOB

Code
>>>del df['DOB'] df
Output

25. Converting CGPA into percentage multiplying it by 10 Code:

Code
>>> df[‘CGPA']*10

Output

26. Sorting the data frame in ascending order based on CGPA Code:

Code
>>> df.sort_values(by='CGPA',ascending = True)

Output
27. Filtering the data those who have CGPA greater than 8 . You can create very
powerful and sophisticated expression by combining logical operations with the
following operators:

• NOT(~)
• AND(&)
• OR( | )
• XOR (^)

Code
>>> df[(df[‘CGPA']>8)]

Output

28. Filtering the data those who have CGPA greater than 8 and age is less than 19

Code
>>> df[(df[‘CGPA']>=8)&(df['Age']<19)]

Output

29. Describing the data , so that it displays mean ,standard deviation, minimum,
maximum and quartiles of column.

Code
>>> df.describe()
Output

Code
>>>df.mean()

Output

Code
>>> df[‘CGPA'].mean()

Output
8.283333333333333

Code
>>> df.std()

Output

Code
>>> df['CGPA'].std()

Output

1.1070983093956321
30. Filter the CGPA of the student greater than 7 CGPA and print the Dataframe

Code
>>> filter_ = df['CGPA'] >= 7 df[filter_]
Output

Conclusion

Thus the various features of Pandas are learned and practiced in Python successfully.

You might also like