Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 In this article, I am explaining different view functions of pandas?

head():-  this function is used to return nth-number of rows of data frame and series both.

Syntax: Dataframe.head(n=5)

Parameters:
n: integer value, number of rows to be returned

Return type: Dataframe with top n rows


if we did not write any parameter on the head then it returns the top 5 records?


import pandas as pd


# making data frame

data = pd.read_csv("nba.csv")


# calling head() method

# storing in new variable

data_top = data.head()


# display

data_top


Convert data frame to series using pandas?

import pandas as pd


# making data frame

data = pd.read_csv("nba.csv")


# number of rows to return

n = 9


# creating series

series = data["Name"]

# returning top n rows

top = series.head(n = n)

# display

print(top)


display rows from bottom?

pandas provide tail() to display row from bottom 


# importing pandas module

import pandas as pd


# making data frame

data = pd.read_csv("nba.csv")


# calling head() method

# storing in new variable

data_top = data.tail()


# display

data_top


# importing pandas module

import pandas as pd


# making data frame

data = pd.read_csv("nba.csv")


# number of rows to return

n = 2


# creating series

series = data["Name"]


# returning top n rows

top = series.tail(n = n)


# display

top



Statistical operation using pandas?

If we want to perform max, min,avg, std functionality then we can use describe()  in pandas.

Syntax: DataFrame.describe(percentiles=None, include=None, exclude=None)

Parameters:
percentile: list-like data type of numbers between 0-1 to return the respective percentile
include: List of data types to be included while describing data frame. Default is None
exclude: List of data types to be Excluded while describing data frame. Default is None

Return type: Statistical summary of the data frame.


,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Projection or Show particular data :-


It is used to show particular data  from data frame


# Import pandas package

import pandas as pd


# Define a dictionary containing employee data

data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

'Age':[27, 24, 22, 32],

'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

'Qualification':['Msc', 'MA', 'MCA', 'Phd']}


# Convert the dictionary into DataFrame

df = pd.DataFrame(data)


# select two columns

print(df[['Name', 'Qualification']])


How to add a new column attribute in the data frame?

# Import pandas package

import pandas as pd


# Define a dictionary containing Students data

data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],

'Height': [5.1, 6.2, 5.1, 5.2],

'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}


# Convert the dictionary into DataFrame

df = pd.DataFrame(data)


# Declare a list that is to be converted into a column

address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']


# Using 'Address' as the column name

# and equating it to the list

df['Address'] = address


# Observe the result

print(df)




Select data based on rows?

# importing pandas package

import pandas as pd


# making data frame from csv file

data = pd.read_csv("nba.csv", index_col ="Name")


# retrieving row by loc method

first = data.loc["Avery Bradley"]

second = data.loc["R.J. Hunter"]



print(first, "\n\n\n", second)



How to merge rows in pandas?


import pandas as pd 
    
# making data frame 
df = pd.read_csv("nba.csv", index_col ="Name"
  
df.head(10)
  
new_row = pd.DataFrame({'Name':'Geeks', 'Team':'Boston', 'Number':3,
                        'Position':'PG', 'Age':33, 'Height':'6-2',
                        'Weight':189, 'College':'MIT', 'Salary':99999},
                                                            index =[0])
# simply concatenate both dataframes
df = pd.concat([new_row, df]).reset_index(drop = True)
df.head(5)






Deletion of rows in Django:-


import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("nba.csv", index_col ="Name" )
  
# dropping passed values
data.drop(["Avery Bradley", "John Holland", "R.J. Hunter","R.J. Hunter"], inplace = True)
  
# display
data



Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post