在我的数据框中:。
df = pd.DataFrame(zip(datetimes, from_, message), columns=['timestamp', 'sender', 'message'])
df['timestamp'] = pd.to_datetime(df.timestamp, format='%d/%m/%Y, %I:%M %p')
有一些有问题的值,由清晰的模式定义:
timestamp sender message
113381 2020-06-04 11:59:24 Jose bom te ver feliz\r\n
113382 2020-06-04 11:59:29 Jose ❤\r\n
113383 2020-06-04 11:59:40 Maria Estar bem com você me faz feliz\r\n
113384 2020-06-04 12:00:57 Maria Estava falando com uma amiga de infância aque...
113385 2020-06-04 12:01:14 Maria Ela teve uma briga feia com o marido\r\n
113386 2020-06-04 12:01:24 Maria: <attached 00113509-PHOTO-2020-06-04-12-01-25.jpg>\r\n
113387 2020-06-04 12:02:54 Maria e assim leva-se a vida, um\n
113388 2020-06-04 12:03:21 Maria Pelo menos ela riu isso ajuda\r\n
113389 2020-06-04 13:06:39 Jose: <attached 00113512-PHOTO-2020-06-04-13-06-40.jpg>\r\n
名称始终会有所不同,并且很可能是
John
John: <attached
Mary
Mary: <attached
但是: <attached
将永远在那里。
我如何执行字符串替换,从而独立于字符串而纠正 ,最后以:
timestamp sender message
113381 2020-06-04 11:59:24 Jose bom te ver feliz\r\n
113382 2020-06-04 11:59:29 Jose ❤\r\n
113383 2020-06-04 11:59:40 Maria Estar bem com você me faz feliz\r\n
113384 2020-06-04 12:00:57 Maria Estava falando com uma amiga de infância aque...
113385 2020-06-04 12:01:14 Maria Ela teve uma briga feia com o marido\r\n
113386 2020-06-04 12:01:24 Maria 00113509-PHOTO-2020-06-04-12-01-25.jpg>\r\n
113387 2020-06-04 12:02:54 Maria e assim leva-se a vida, um\n
113388 2020-06-04 12:03:21 Maria Pelo menos ela riu isso ajuda\r\n
113389 2020-06-04 13:06:39 Jose 00113512-PHOTO-2020-06-04-13-06-40.jpg>\r\n
答案 0 :(得分:2)
这应该可行;
df['sender'] = df['sender'].str.replace(u': \u200e<attached', '')
答案 1 :(得分:2)
数据
df = pd.DataFrame({'sender': ['Jose','Jose','Maria','Maria','Maria','Maria: <attached','Maria','Maria','Jose: <attached']})
解决方案
df.sender = df.sender.str.split(': <attached').str[0]
sender
0 Jose
1 Jose
2 Maria
3 Maria
4 Maria
5 Maria
6 Maria
7 Maria
8 Jose
答案 2 :(得分:2)
8位Borges,您的数据中可能包含\u200e
字符。我遇到了类似的问题,因为像这样的奇怪字符,split什么也不做。这是我的解决方案:
a = df['sender'].to_dict()
然后,我看到了将其发送到字典时的实际值。该值为: \u200e<attached
。然后,我只是做了:
df['sender'] = df['sender'].str.split(': \u200e<attached').str[0]
此处有关\u200e
的更多信息:decoding \u200e to string