如何将.findall()函数用于遍历列的所有行的excel文件?

2024-05-15 13:24:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很大的excel表格,每个公司的一个单元格里都有不同公司的信息,我的目标是按照第一列的模式将这些信息分成不同的列。原始数据如下所示:

enter image description here

我的目标是实现如下数据帧:

enter image description here

我创建了以下代码来使用模式Mr、、Affiliation:、E-mail:、Mobile,因为它们在每一行中都以相同的方式重复。但是,我不知道如何使用findall()函数从所需列的每一行中提取所有需要的信息。你知道吗

import openpyxl
import re
import sys  
import pandas as pd
reload(sys)  
sys.setdefaultencoding('utf8')
wb = openpyxl.load_workbook('/Users/ap/info1.xlsx')
ws = wb.get_sheet_by_name('Companies')
w={'Name': [],'Affiliation': [], 'Email':[]}
for row in ws.iter_rows('C{}:C{}'.format(ws.min_row,ws.max_row)):
    for cells in row:
        a=re.findall(r'Mr.(.*?)Affiliation:',aa, re.DOTALL)
        a1="".join(a).replace('\n',' ')
        b=re.findall(r'Affiliation:(.*?)E-mail',aa,re.DOTALL)
        b1="".join(b).replace('\n',' ')
        c=re.findall(r'E-mail(.*?)Mobile',aa,re.DOTALL)
        c1="".join(c).replace('\n',' ')
        w['Name'].append(q1)
        w['Affiliation'].append(r1)
        w['Email'].append(s1)
        print cell.value

df=pd.DataFrame(data=w)
df.to_excel(r'/Users/ap/info2.xlsx')  

Tags: importre信息wssysmailexcelreplace
1条回答
网友
1楼 · 发布于 2024-05-15 13:24:40

我会用这个,它代替了“E”-邮件:。。。,然后拆分并分配给右列

df['Name'] = np.nan
df['Affiliation'] = np.nan
df['Email'] = np.nan
df['Mobile'] = np.nan

for i in range(0, len(df)):
    full_value = df['Companies'].loc[i]
    full_value = full_value.replace('Affiliation:', ';').replace('E-mail:', ';').replace('Mobile:', ';')
    full_value = full_value.split(';')
    df['Name'].loc[i] = full_value[0]
    df['Affiliation'].loc[i] = full_value[1]
    df['Email'].loc[i] = full_value[2]
    df['Mobile'].loc[i] = full_value[3]

del df['Companies']
print(df)

相关问题 更多 >