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

Section 8

Uploaded by

Rafeeq 2
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)
12 views18 pages

Section 8

Uploaded by

Rafeeq 2
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

Section 9

1. Add a row to a DataFrame and display the data:


• Add a new row with specific values to an existing DataFrame, then
display the result.
2. Delete a row from a DataFrame using the
index:
• Delete the row at index 1 from a DataFrame.
3. Add a new column with specific values:
• Add a new column called "City" to a DataFrame and fill it with the
value "Cairo".
4. Delete a column from a DataFrame:
• Delete the "Age" column from a DataFrame.
5. Insert a column in a specific position within a
DataFrame:
• Insert a new column filled with NA at the second position in a
DataFrame.
6. Rename a column in a DataFrame:
• Rename the column "Name" to "Full Name".
7. Replace values in a specific column:
• Change all values in the "Age" column to 10,20,30.
5. Filter rows with conditions:
• How can you filter rows where the age is greater than 25?
5. Filter rows with multiple conditions:
• How can you find rows where the age is greater than 25 and the
city is "Cairo"?
7. Count how many rows match a condition:
• How can you count the number of rows where the age is less than
30?
Original Data
Ex 1: Import excel data into a data frame and display the first five rows:

import pandas as pd
w=pd.read_excel("[Link]")
print([Link](5))

Ex 2:Import excel data into a Pandas data frame and display the last ten rows:

import pandas as pd
w=pd.read_excel("[Link]")
print([Link](10))
Ex 3 : Import sheet2 from the excel sheet into a Pandas data frame:

import pandas as pd
w=pd.read_excel("[Link]",sheet_name=1)
print(w)

Ex 4 : Find the sum, max, min, mean value of given column: Varname[“colname”].funna
me
import pandas as pd
w=pd.read_excel("[Link]",sheet_name=1)
print("sum= ", w["s3r"].sum())
print("max= ", w["s3r"].max())
print("min= ", w["s3r"].min())
print("mean= ", w["s3r"].mean())
Ex 5 : Find a specific ID:

import pandas as pd
w=pd.read_excel("[Link]")
print(w[w["id"]==103246])

W[“id”] : retrieves the values in that column


W[“id]==103246: create Boolean (array), check value of id = 103246 ( T equal / F not equal)

(w[w["id"]==103246]) : use Boolean array as an index to filter df by the values.


Ex 6: Read specific columns from a given excel file:
import pandas as pd
w=pd.read_excel("[Link]",usecols=[0,2,3])
print(w)

Ex 7 : Insert a column in specified position in a excel sheet


and fill it with NaN values:display the first five rows:
import pandas as pd
df = pd.read_excel("[Link]")
[Link](1, "column", [Link])
print([Link](5))

Insert(location(index), colname, value (na as null value)

You might also like