简体   繁体   English

在非ASCII字符上拆分数据框列

[英]Split a dataframe column on non-ASCII characters

This is a column with data and non ascii characters 这是包含数据和非ASCII字符的列

Summary 1

United Kingdom - ��Global Consumer Technology - ��American Express 
United Kingdom - ��VP Technology - Founder - ��Hogarth Worldwide
Aberdeen - ��SeniorCore Analysis Specialist - ��COREX Group
London, - ��ED, Equit Technology, London - ��Morgan Stanley
United Kingdom - ��Chief Officer, Group Technology - ��BP

How split them and save in different column 如何拆分它们并保存在不同的列中

The code i used is: 我使用的代码是:

import io
import pandas as pd

df = pd.read_csv("/home/vipul/Desktop/dataminer.csv", sep='\s*\+.*?-\s*')
df = df.reset_index()
df.columns = ["First Name", "Last Name", "Email", "Profile URL", "Summary 1", "Summary 2"]

df.to_csv("/home/vipul/Desktop/new.csv")

Say, you have a column in a series like this: 假设您在这样的系列中有一个专栏:

s

0    United Kingdom - ��Global Consumer Technolog...
1    United Kingdom - ��VP Technology - Founder -...
2    Aberdeen - ��SeniorCore Analysis Specialist ...
3    London, - ��ED, Equit Technology, London - �...
4    United Kingdom - ��Chief Officer, Group Tech...
Name: Summary 1, dtype: object

Option 1 选项1
Expanding on this answer , you can split on non-ascii characters using str.split : 扩展这个答案 ,您可以使用str.split分割非ASCII字符:

s.str.split(r'-\s*[^\x00-\x7f]+', expand=True)

                 0                                 1                  2
0  United Kingdom        Global Consumer Technology    American Express
1  United Kingdom           VP Technology - Founder   Hogarth Worldwide
2        Aberdeen    SeniorCore Analysis Specialist         COREX Group
3         London,      ED, Equit Technology, London      Morgan Stanley
4  United Kingdom   Chief Officer, Group Technology                  BP

Option 2 选项2
str.extractall + unstack : str.extractall + unstack

s.str.extractall('([\x00-\x7f]+)')[0].str.rstrip(r'- ').unstack()

match               0                                1                  2
0      United Kingdom       Global Consumer Technology   American Express
1      United Kingdom          VP Technology - Founder  Hogarth Worldwide
2            Aberdeen   SeniorCore Analysis Specialist        COREX Group
3             London,     ED, Equit Technology, London     Morgan Stanley
4      United Kingdom  Chief Officer, Group Technology                 BP

Another approach : 另一种方法:

a
0   United Kingdom - ��Global Consumer Technolog...
1   United Kingdom - ��VP Technology - Founder -...
2   Aberdeen - ��SeniorCore Analysis Specialist ...
3   London, - ��ED, Equit Technology, London - �...
4   United Kingdom - ��Chief Officer, Group Tech...

Use this function to extract assci char (where Unicode code point is superior to 128 ) using ord build-in function 使用此函数通过ord内置函数提取assci char(Unicode代码点优于128)

def extract_ascii(x):
    string_list = filter(lambda y : ord(y) < 128, x)
    return ''.join(string_list)

and apply it to columns. 并将其应用于列。

df1.a.apply(extract_ascii).str.split('-', expand=True)

here is the results : 结果如下:

             0          1                              2           3
0   United Kingdom  Global Consumer Technology  American Express    None
1   United Kingdom  VP Technology   Founder Hogarth Worldwide
2   Aberdeen    SeniorCore Analysis Specialist  COREX Group None
3   London, ED, Equit Technology, London    Morgan Stanley  None
4   United Kingdom  Chief Officer, Group Technology BP  None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM