如何使用Pandas从Excel文件中提取Email列并找出邮件的类型

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型

在这篇文章中,让我们看看如何使用Pandas从Excel文件中提取电子邮件列并找出邮件的类型。假设我们的Excel文件看起来像下面给出的图片,然后我们必须在数据框架的不同列中存储不同类型的邮件。

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型?

步骤:

  • 导入所需模块。
  • 从Excel文件导入数据。
  • 为每个不同的Email多做一列。
  • 设置每个所需的索引进行搜索。
  • 定义电子邮件的模式。
  • 搜索电子邮件并将其分配给数据框架中的相应列。

让我们来看看一步一步的实现情况。

第1步:导入所需的模块并从Excel文件中读取数据。

# import required module
import pandas as pd;
import re;
  
# Read excel file and store in to DataFrame
data = pd.read_excel("Email_sample.xlsx");
  
# show the dataframe
data
Python

输出:

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型?

第二步:为每个不同的电子邮件做一个额外的列。

data['Google-mail'] = None
  
data
Python

输出:

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型?

data['Yahoo-mail'] = None
data
Python

输出 :

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型?

第3步:设置每个所需的索引进行搜索。

# set required index 
index_set = data.columns.get_loc('E-mail')
index_gmail = data.columns.get_loc('Google-mail')
index_yahoo = data.columns.get_loc('Yahoo-mail')
  
print(index_set, index_gmail, 
      index_yahoo)
Python

输出:

1 2 3
Python

第四步:确定电子邮件的模式。

# define pattern of Email
google_pattern = r'gmail.com'
yahoo_pattern = r'yahoo.com'
Python

第5步:搜索电子邮件并将其分配到数据框架中的相应列。

# Search the Email in DataFrame and store  
for row in range(0, len(data)):
    
    if re.search(google_pattern,
                 data.iat[row, index_set]) == None :
        data.iat[row,index_gmail] = 'Account not belongs to Google'
          
    else:
        gmail = re.search(google_pattern,
                          data.iat[row, index_set]).group()
        data.iat[row,index_gmail] = "Google-Mail"
  
    if re.search(yahoo_pattern,
                 data.iat[row, index_set]) == None :
        data.iat[row,index_yahoo] = 'Account not belongs to Yahoo'
          
    else:
        yahoo = re.search(yahoo_pattern,
                          data.iat[row, index_set]).group()
        data.iat[row,index_yahoo] = "Yahoo-Mail"
          
data
Python

输出:

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型?

Complete 代码:

# importing required module
import pandas as pd
import re
  
# Creating df
# Reading data from Excel
data = pd.read_excel("Email_sample.xlsx")
print("Original DataFrame")
print(data)
  
# Create column for
# each type of Email
data['Google-mail'] = None
data['Yahoo-mail'] = None
  
# set index
index_set = data.columns.get_loc('E-mail')
index_gmail = data.columns.get_loc('Google-mail')
index_yahoo = data.columns.get_loc('Yahoo-mail')
  
# define Email pattern
google_pattern = r'gmail.com'
yahoo_pattern = r'yahoo.com'
  
# Searching the email
# Store into DataFrame
for row in range(0, len(data)):
    if re.search(google_pattern,
                 data.iat[row, index_set]) == None:
        data.iat[row, index_gmail] = 'Account not belongs to Google'
    else:
        gmail = re.search(google_pattern,
                          data.iat[row, index_set]).group()
        data.iat[row, index_gmail] = "Google-Mail"
  
    if re.search(yahoo_pattern,
                 data.iat[row, index_set]) == None:
        data.iat[row, index_yahoo] = 'Account not belongs to Yahoo'
    else:
        yahoo = re.search(yahoo_pattern,
                          data.iat[row, index_set]).group()
        data.iat[row, index_yahoo] = "Yahoo-Mail"
  
data
Python

输出 :

如何使用Pandas从Excel文件中提取Email列并找出邮件的类型?

注意:在运行这个程序之前,确保你已经在你的Python环境中安装了xlrd库。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册