python - Pandas - 用特定模式替换值

标签 python pandas

在我的数据框中:.

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

最佳答案

数据

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

关于python - Pandas - 用特定模式替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226155/

相关文章:

pandas - 基于子级别的分层索引

python - pandas 数据框中一列中的数字分组

python - UnicodeEncodeError : 'charmap' codec can't encode character. ..问题

python - 如何在 python 中查找每 n ( 50 ) 行的模式?

python - 如何克隆一个列表以使其在分配后不会意外更改?

python - 谷歌电子表格 API : 'SpreadsheetsClient' object has no attribute 'update_cell'

python - 如果值在 col2 中但不在 Python Pandas 的列表中,如何从 col1 中选择值?

pandas - 用不同颜色绘制 1000 行的两列

python - tensorflow GPU安装问题

python - 没有名为 pkg_resources 的模块