0% found this document useful (0 votes)
11 views2 pages

Experiment 5 To 8

The document contains code to perform several tasks with Python: 1) Concatenate DataFrames with different indexes and columns 2) Check that a string contains only certain characters (a-z, A-Z, 0-9) 3) Validate an email address with a regular expression 4) Sort a list of people by age (youngest first) and print their names, using a decorator to sort the list before calling the name printing function.

Uploaded by

pandikumar.s
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)
11 views2 pages

Experiment 5 To 8

The document contains code to perform several tasks with Python: 1) Concatenate DataFrames with different indexes and columns 2) Check that a string contains only certain characters (a-z, A-Z, 0-9) 3) Validate an email address with a regular expression 4) Sort a list of people by age (youngest first) and print their names, using a decorator to sort the list before calling the name printing function.

Uploaded by

pandikumar.s
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/ 2

5.

Write a python program to concatenate the data frames with two different objects

Coding:

import pandas as pd

import numpy as np

df1 = pd.DataFrame(np.random.randint(25, size=(4, 4)), index=["1", "2", "3", "4"],columns=["A", "B",


"C", "D"])

df2 = pd.DataFrame(np.random.randint(25, size=(6, 4)), index=["5", "6", "7", "8", "9", "10"],
columns=["A", "B", "C", "D"])

df3 = pd.DataFrame(np.random.randint(25, size=(4, 4)), columns=["A", "B", "C", "D"])

df4 = pd.DataFrame(np.random.randint(25, size=(4, 4)), columns=["E", "F", "G", "H"])

display(df1, df2, df3, df4)

6. Write a Python program to check that a string contains only a certain set of characters (in this case
a-z, A-Z and 0-9). Go tothe editor

Coding:

import re

def is_allowed_specific_char(string):

charRe = re.compile(r'[^a-zA-Z0-9]')

string = charRe.search(string)

return not bool(string)

print(is_allowed_specific_char("ABCDEFabcdef123450"))

print(is_allowed_specific_char("*&%@#!}{"))

7. Write a python program to check the valid email id

Code:
import re
def validate_email(email):
if re.match(r"[^@]+@[^@]+\.[^@]+", email):
return True
return False
email = "[email protected]"
if validate_email(email):
print("Valid email address")
else:
print("Invalid email address")
8. Build a name directory using decorators for a given piece of information about N
people where every person’sname has first and last name, age, and sex. Sort their
ages in ascending order and print their names accordingly. So, the name of the
youngest person should be printed first. If there are two people of the same age,
then you print them in the order of their input.

Code:
def sort_age(func):
def inner(persons):
persons = sorted(persons, key = lambda x: x[1])
return [func(name, sex) for (name, age, sex) in persons]
return inner
@sort_age
def list_name(name, sex):
title = {"M": "Mr. ", "F": "Ms. "}
return title[sex] + name
if __name__ == "__main__":
persons = []
for i in range(input()):
data = raw_input().split()
name, age, sex = data[0] + ' ' + data[1], int(data[2]), data[3]
persons.append([name, age, sex])

print('\n'.join(list_name(persons))

You might also like