熊猫解析文本列
我有一个 csv 表,其中有一列包含聊天日志中的文本。每个文本行都遵循相同的格式:消息的人员姓名和时间(带有额外的前后空格填充),后跟消息内容。文本列的单行示例:
' Siri (3:15pm) Hello how can I help you? John Wayne (3:17pm) what day of the week is today Siri (3:18pm) it is Monday.'
我想将这个单个字符串列转换为多列(列数取决于消息数量),每条消息对应一列,如下所示:
Siri (下午 3:15)您好,需要什么帮助吗
John Wayne (下午 3:17) 今天是星期几
Siri (下午 3:18) 今天是星期一
Siri (下午 3:18) 今天是星期一
代码>
我如何解析这段文本pandas dataframe 列将聊天日志分成单独的消息列?
I have a csv table with a column that contains the text from a chat log. Each text row follows the same format of the name of the person and time of the message (with an additional front and back space padding) followed by the message content. An example of a single row of the text column:
' Siri (3:15pm) Hello how can I help you? John Wayne (3:17pm) what day of the week is today Siri (3:18pm) it is Monday.'
I would like to transform this single string column, into multiple columns (number of columns would depend on number of messages), with one column for each individual message like below:
Siri (3:15pm) Hello how can I help you
John Wayne (3:17pm) what day of the week is today
Siri (3:18pm) it is Monday
How can I parse this text in a pandas dataframe column to separate the chat logs into individual message columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有此数据框:
那么您可以执行以下操作:
打印:
注意:仅当名称和文本之间有 2 个以上空格时才有效。
If you have this dataframe:
then you can do:
Prints:
Note: It only works if there 2+ spaces between the Name and Text.
这就是我的做法,花了我一段时间,但我们做到了!
This is how I did it, took me a while but we got to it!