Sit*_*ogz 1 python regex dataframe pandas data-cleaning
我有一个包含x行和y列数的大型数据集.其中一列作为单词和一些不需要的数据.不需要的数据没有特定的模式,因此我发现很难从数据帧中删除它.
nonhashtag
['want', 'better', 'than', 'Dhabi,', 'United', 'Arab', 'Emirates']
['Just', 'posted', 'photo', 'Rasim', 'Villa']
['Dhabi', 'International', 'Airport', '(AUH)', '\xd9\x85\xd8\xb7\xd8\xa7\xd8\xb1', '\xd8\xa3\xd8\xa8\xd9\x88', '\xd8\xb8\xd8\xa8\xd9\x8a', '\xd8\xa7\xd9\x84\xd8\xaf\xd9\x88\xd9\x84\xd9\x8a', 'Dhabi']
['just', 'shrug', 'off!', 'Dubai', 'Mall', 'Burj', 'Khalifa']
['out!', 'Cowboy', 'steppin', 'Notorious', 'going', 'sleep!', 'Make', 'happen']
['Buona', 'notte', '\xd1\x81\xd0\xbf\xd0\xbe\xd0\xba\xd0\xbe\xd0\xb9\xd0\xbd\xd0\xbe\xd0\xb9', '\xd0\xbd\xd0\xbe\xd1\x87\xd0\xb8', '\xd9\x84\xd9\x8a\xd9\x84\xd8\xa9', '\xd8\xb3\xd8\xb9\xd9\x8a\xd8\xaf\xd8\xa9!', '\xd8\xa3\xd8\xa8\xd9\x88', '\xd8\xb8\xd8\xa8\xd9\x8a', 'Viceroy', 'Hotel,', 'Yas\xe2\x80\xa6']
Run Code Online (Sandbox Code Playgroud)
每个不是单词的字符都将被删除,这只是大数据集中的一列.列名是nonhashtag
清洁色谱柱的简单方法是什么?直接将它们取下或更换NAN
预期产出
nonhashtag
['want', 'better', 'than', 'Dhabi,', 'United', 'Arab', 'Emirates']
['Just', 'posted', 'photo', 'Rasim', 'Villa']
['Dhabi', 'International', 'Airport', '(AUH)', 'Dhabi']
['just', 'shrug', 'off!', 'Dubai', 'Mall', 'Burj', 'Khalifa']
['out!', 'Cowboy', 'steppin', 'Notorious', 'going', 'sleep!', 'Make', 'happen']
['Buona', 'notte', 'Viceroy', 'Hotel,']
Run Code Online (Sandbox Code Playgroud)
每个[]都是该特定列中的一行,因此仅\x and remaining characters需要删除空行[]应该留在行中.保持行很重要,因为其他列的行充满了所需的信息.
要编写正确的代码,我无法通过输入读取,因为我无法在数据集中找到编写正则表达式的模式.
在此先感谢您的帮助
那是你要的吗?
In [71]: df.nonhashtag.apply(' '.join).str.replace('[^A-Za-z\s]+', '') \
.str.split(expand=False)
Out[71]:
0 [want, better, than, Dhabi, United, Arab, Emir...
1 [Just, posted, photo, Rasim, Villa]
2 [Dhabi, International, Airport, AUH, Dhabi]
3 [just, shrug, off, Dubai, Mall, Burj, Khalifa]
4 [out, Cowboy, steppin, Notorious, going, sleep...
5 [Buona, notte, Viceroy, Hotel, Yas]
Name: nonhashtag, dtype: object
Run Code Online (Sandbox Code Playgroud)
'[^A-Za-z\s]+'- 是一个RegEx意思是除了那些之外的所有字符:
A到Za到z因此,.str.replace('[^A-Za-z\s]+', '')除了属于英文字母,空格和制表符的字母外,将删除所有字符
我导入很多文件,很多时候列名很脏,它们会出现不需要的特殊字符,而且我不知道可能会出现哪些字符。我只想在列名称中使用下划线并且不包含空格
df.columns = df.columns.str.strip()
df.columns = df.columns.str.replace(' ', '_')
df.columns = df.columns.str.replace(r"[^a-zA-Z\d\_]+", "")
df.columns = df.columns.str.replace(r"[^a-zA-Z\d\_]+", "")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7989 次 |
| 最近记录: |