Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
63 views3 pages

This PDF Is Created at

This document discusses various operations that can be performed on rows and columns in a Pandas dataframe, including selecting, adding, deleting, and sorting rows and columns. Specific examples are provided for selecting columns and rows, adding a new column, deleting columns and rows, and sorting dataframes based on column values.

Uploaded by

Manish Jain
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)
63 views3 pages

This PDF Is Created at

This document discusses various operations that can be performed on rows and columns in a Pandas dataframe, including selecting, adding, deleting, and sorting rows and columns. Specific examples are provided for selecting columns and rows, adding a new column, deleting columns and rows, and sorting dataframes based on column values.

Uploaded by

Manish Jain
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/ 3

We can perform basic operations on rows/columns like selecting, deleting,

adding, and renaming


Selecting Column:
# 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']])
adding col in dataframe:

# 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)
Column Deletion:

brightness_4
import pandas as pd
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Height': [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

df = pd.DataFrame(data)
# dropping passed columns
df.drop(["Name", "Height"], axis = 1, inplace = True)

# display
print(df)
Row Selection:

This PDF is created at https://www.pdfonline.com/convert-pdf/


# importing pandas package
# importing pandas module
import pandas as pd
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Height': [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

df = pd.DataFrame(data)

print(df)
# retrieving row by loc method
first = df.loc[0]
second = df.loc[1]

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

Row Deletion:
# importing pandas module
import pandas as pd
data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Height': [5.1, 6.2, 5.1, 5.2],
'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

df = pd.DataFrame(data)

print(df)
df.drop([0], inplace = True)
print(df)
condition on row records
# importing pandas
import pandas as pd

record = {

'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],


'Age': [21, 19, 20, 18, 17, 21],
'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
'Percentage': [88, 92, 95, 70, 65, 78] }

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream',
'Percentage'])

print("Given Dataframe :\n", dataframe)

# selecting rows based on condition


rslt_df = dataframe[dataframe['Percentage'] > 80]

print('\nResult dataframe :\n', rslt_df)


rslt_df.drop([0,1]) # droping multiple row
*sorting*:
print(df.sort_values(by=['Name']))

This PDF is created at https://www.pdfonline.com/convert-pdf/


print(df.sort_values(by=['Name','Age']))
df.sort_values(by='Name', ascending=False)

This PDF is created at https://www.pdfonline.com/convert-pdf/

You might also like