Python Pandas program to join the two Dataframes along rows

In this python pandas program, we will join the two Dataframes along rows using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create two dataframes using pd.DataFrame().
  3. Join the dataframes using pd.concat().
  4. Print the output.
				
					import pandas as pd
df1 = pd.DataFrame({'ID':[1,2,3],'Name':['Yash','Gaurav','Sanket'],
                   'Age':[30,27,28]})
df2 = pd.DataFrame({'ID':[4,3],'Name':['Tanmay','Athrva'],'Age':[26,22]})
result = pd.concat([df1,df2])
print("New dataframe")
print(result)
				
			

Output :

				
					New dataframe
   ID    Name  Age
0   1    Yash   30
1   2  Gaurav   27
2   3  Sanket   28
0   4  Tanmay   26
1   3  Athrva   22
				
			

extract only words from a column of a DataFrame

join the two Dataframes along columns

Extract only words from a column of a DataFrame

In this python pandas program, we will extract only words from a column of a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import re library.
  3. Create a dataframe using pd.DataFrame().
  4. Create a function to extract only words from the record.
  5. Extract only words from the record using re.findall(r’\b[^\d\W]+\b’, text).
  6. It will extract words from the record and the return will give words as the output of the function.
  7. Now apply this function to the address column of the dataframe using df[‘Address’].apply(lambda x : search_words(x)).
  8. The lambda function will apply the created function to each row to extract words from the address and store it in the new column.
  9. Print the output.
				
					import pandas as pd
import re
d = {'Name':['Ramesh','Suresh','Sanket'],
     'Address':['297 shukrawar peth','200 ravivar peth','090 shanivar peth']}
df = pd.DataFrame(d)
print(df)
def search_words(text):
    result = re.findall(r'\b[^\d\W]+\b', text)
    return " ".join(result)

df['words']=df['Address'].apply(lambda x : search_words(x))
print("Only words:")
print(df)
				
			

Output :

				
					0     Name             Address
0  Ramesh  297 shukrawar peth
1  Suresh    200 ravivar peth
2  Sanket   090 shanivar peth
Only words:
     Name             Address           words
0  Ramesh  297 shukrawar peth  shukrawar peth
1  Suresh    200 ravivar peth    ravivar peth
2  Sanket   090 shanivar peth   shanivar peth
				
			

extract the hash attached word from Twitter text from the specified column of a given DataFrame

join the two Dataframes along rows

Extract the hash attached word from a tweet

In this python pandas program, we will extract the hash attached word from a tweet using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import re library.
  3. Create a dataframe using pd.DataFrame().
  4. Create a function to extract the hash attached word from the record.
  5. Extract the hash attached word from the record using re.findall(r'(?<=#)\w+’,text).
  6. It will extract the hash attached word from the record and the return will give word as the output of the function.
  7. Now apply this function to the Tweets column of the dataframe using df[‘Tweets’].apply(lambda x: find_hash(x)).
  8. The lambda function will apply the created function to each row to extract the hash attached word and store it in the new column.
  9. Print the output.
				
					import pandas as pd
import re
d = {'Tweets':['Pune #love','#boycottmovie','enjoying #peace']}
df = pd.DataFrame(d)
print(df)
def find_hash(text):
    hword=re.findall(r'(?<=#)\w+',text)
    return " ".join(hword)
df['extracted_word']=df['Tweets'].apply(lambda x: find_hash(x))
print("Extracting#@word from dataframe columns:")
print(df)
				
			

Output :

				
					0            Tweets
0       Pune #love
1    #boycottmovie
2  enjoying #peace
Extracting#@word from dataframe columns:
            Tweets extracted_word
0       Pune #love           love
1    #boycottmovie   boycottmovie
2  enjoying #peace          peace
				
			

extract email from a specified column of a given DataFrame

extract only words from a column of a DataFrame

Extract email from a column of the DataFrame

In this python pandas program, we will extract email from a column of the DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import re library.
  3. Create a dataframe using pd.DataFrame().
  4. Create a function to extract email from the record.
  5. Extract email from the record using re.findall(r'[\w\.-]+@[\w\.-]+’,str(text)).
  6. It will extract email from the record and return will give email as the output of the function.
  7. Now apply this function to the company name column of the dataframe using df[‘Company_mail’].apply(lambda x: find_email(x)).
  8. The lambda function will apply the created function to each row to extract email and store it in the new column.
  9. Print the output.
				
					import pandas as pd 
import re
d = {'Company_mail':['TCS tcs@yahoo.com','Apple apple@icloud.com','Google google@gmail.com']}
df = pd.DataFrame(d)
print(df)
def find_email(text):
    email = re.findall(r'[\w\.-]+@[\w\.-]+',str(text))
    return ",".join(email)
df['email']=df['Company_mail'].apply(lambda x: find_email(x))
print("Extracting email from dataframe columns:")
print(df)
				
			

Output :

				
					0              Company_mail
0        TCS tcs@yahoo.com
1   Apple apple@icloud.com
2  Google google@gmail.com
Extracting email from dataframe columns:
              Company_mail             email
0        TCS tcs@yahoo.com     tcs@yahoo.com
1   Apple apple@icloud.com  apple@icloud.com
2  Google google@gmail.com  google@gmail.com
				
			

get the length of the integer of a column in a DataFrame

extract the hash attached word from Twitter text from the specified column of a given DataFrame

Get the length of the integer of a column in a DataFrame

In this python pandas program, we will get the length of the integer of a column in a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Create a new column that will contain the length of the integers in the sales column using df[‘Sales’].map(str).apply(len).
  4. It will convert the integers into strings and then apply the len() function to calculate the length of the integers.
  5. Print the output.
				
					import pandas as pd
df = pd.DataFrame({'Sales':[55000,75000,330000,10000]})
print("Original DataFrame:")
print(df)
print("Length of sale_amount:")
df['Length'] = df['Sales'].map(str).apply(len)
print(df)
				
			

Output :

				
					Original DataFrame:
    Sales
0   55000
1   75000
2  330000
3   10000
Length of sale_amount:
    Sales  Length
0   55000       5
1   75000       5
2  330000       6
3   10000       5
				
			

check whether only alphabetic values present in a column of a DataFrame

extract email from a specified column of a given DataFrame

Check whether only alphabetic values present in a column of a DataFrame

In this python pandas program, we will only alphabetic values present in a column of a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Check whether only the alphabets are present in a record of a column of a DataFrame using df[‘Characters’] = list(map(lambda x: x.isalpha(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the row contains only the alphabet or else False.
  5. The lambda function will check whether the value in the column contains only the alphabet or not.
  6. map() works as an iterator to return a result after applying a function to every item of an iterable 
  7. list() will create a list of all results after applying the lambda function to each value.
  8. Print the output.
				
					import pandas as pd
d = {'Marks':['Pass','88','First Class','90','Distinction']}
df = pd.DataFrame(d)
df['Characters'] = list(map(lambda x: x.isalpha(), df['Marks']))
print(df)
				
			

Output :

				
					0        Marks  Characters
0         Pass        True
1           88       False
2  First Class       False
3           90       False
4  Distinction        True

				
			

check whether only numeric values are present in a column of a DataFrame

get the length of the integer of a column in a DataFrame

Check whether only numeric values are present in a column of a DataFrame

In this python pandas program, we will check whether only numeric values are present in a column of a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Check whether only the numbers are present in a column of a DataFrame using df[‘Numeric’] = list(map(lambda x: x.isdigit(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the row contains all the numbers characters or else False.
  5. The lambda function will check whether the value in the column contains all the numbers or not.
  6. map() works as an iterator to return a result after applying a function to every item of an iterable 
  7. list() will create a list of all results after applying the lambda function to each value.
  8. Print the output.
				
					import pandas as pd
d = {'Marks':['Pass','88','First Class','90','Distinction']}
df = pd.DataFrame(d)
df['Numeric'] = list(map(lambda x: x.isdigit(), df['Marks']))
print(df)
				
			

Output :

				
					0         Marks  Numeric
0         Pass    False
1           88     True
2  First Class    False
3           90     True
4  Distinction    False
				
			

check whether only the lower case is present in a given column of a DataFrame

check whether only alphabetic values present in a column of a DataFrame

Check whether only the lower case is present in a given column of a DataFrame

In this python pandas program, we will check whether only the lower case is present in a given column of a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Check whether only the lowercase is present in a column of a DataFrame using df[‘Lowerercase’] = list(map(lambda x: x.islower(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the name row contains all the lowercase characters or else False.
  5. The lambda function will check whether the value in the name column contains all the lowercase characters or not
  6. list() will create a list of all results after applying the lambda function to each value.
  7. Print the output.
				
					import pandas as pd
d = {'Name':['kate','jason','ROBERT','MARK','dwyane']}
df = pd.DataFrame(d)
df['Lowerrcase'] = list(map(lambda x: x.islower(), df['Name']))
print(df)
				
			

Output :

				
					0     Name  Lowerrcase
0    kate        True
1   jason        True
2  ROBERT       False
3    MARK       False
4  dwyane        True
				
			

check whether only the upper case is present in a column of a DataFrame

check whether only numeric values are present in a column of a DataFrame

Check whether only the upper case is present in a column of a DataFrame

In this python pandas program, we will check whether only the upper case is present in a column of a DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Check whether only the uppercase is present in a column of a DataFrame using df[‘Uppercase’] = list(map(lambda x: x.isupper(), df[‘Name’])).
  4. It will create a new column in the dataframe that will contain True if the value in the name row contains all the uppercase characters or else False.
  5. The lambda function will check whether the value in the name column contains all the uppercase characters or not
  6. list() will create a list of all results after applying the lambda function to each value.
  7. Print the output.
				
					import pandas as pd
d = {'Name':['Kate','Jason','ROBERT','MARK','Dwyane']}
df = pd.DataFrame(d)
df['Uppercase'] = list(map(lambda x: x.isupper(), df['Name']))
print(df)
				
			

Output :

				
					0     Name  Uppercase
0    Kate      False
1   Jason      False
2  ROBERT       True
3    MARK       True
4  Dwyane      False
				
			

convert all the string values in the DataFrame to uppercases

check whether only the lower case is present in a given column of a DataFrame

Convert all the string values in the DataFrame to uppercases

In this python pandas program, we will convert all the string values in the DataFrame to uppercases using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Convert all the string values in the DataFrame to uppercases using df.str.upper().
  4. Print the output.
				
					import pandas as pd
df = pd.Series(['Cricket','Yes','Pass','Fail'])
print(df)
print("Convert all string values of the Series to upper case:")
print(df.str.upper())
				
			

Output :

				
					0    Cricket
1        Yes
2       Pass
3       Fail
dtype: object
Convert all string values of the Series to upper case:
0    CRICKET
1        YES
2       PASS
3       FAIL
dtype: object
				
			

check whether only the upper case is present in a column of a DataFrame