我有一个带有列的数据帧.在我的模式栏中,有多个以句点结尾的句子.我需要每句话周围有一个双引号.

下面是原文

patterns
Keep related supplies in the same area., Make an effort to clean a dedicated workspace after every session., Place loose supplies in large, clearly visible containers., Use clotheslines and clips to hang sketches, photos, and reference material., Use every inch of the room for storage, especially vertical space., Use chalkboard paint to make space for drafting ideas right on the walls., Purchase a label maker to make your organization strategy semi permanent., Make a habit of throwing out old, excess, or useless stuff each month.

我需要的结果是:

patterns
"Keep related supplies in the same area.", "Make an effort to clean a dedicated workspace after every session.", "Place loose supplies in large, clearly visible containers.", "Use clotheslines and clips to hang sketches, photos, and reference material.", "Use every inch of the room for storage, especially vertical space.", "Use chalkboard paint to make space for drafting ideas right on the walls.", "Purchase a label maker to make your organization strategy semi permanent.", "Make a habit of throwing out old, excess, or useless stuff each month."

已经做过的事,

df['patterns'] = df['patterns'].astype(str)

推荐答案

IIUC,您可以:

df["patterns"] = (
    df["patterns"]
    .str.findall(r"[A-Z].*?(?=,\s*[A-Z]|\Z)")
    .apply(lambda s: ", ".join(map(lambda s: f'"{s}"', s)))
)

打印:

patterns
"Keep related supplies in the same area.", "Make an effort to clean a dedicated workspace after every session.", "Place loose supplies in large, clearly visible containers.", "Use clotheslines and clips to hang sketches, photos, and reference material.", "Use every inch of the room for storage, especially vertical space.", "Use chalkboard paint to make space for drafting ideas right on the walls.", "Purchase a label maker to make your organization strategy semi permanent.", "Make a habit of throwing out old, excess, or useless stuff each month."

Python相关问答推荐

拆分pandas列并创建包含这些拆分值计数的新列

配置Sweetviz以分析对象类型列,而无需转换

如何使用Google Gemini API为单个提示生成多个响应?

使用mySQL的SQlalchemy过滤重叠时间段

将特定列信息移动到当前行下的新行

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

Julia CSV for Python中的等效性Pandas index_col参数

在Python中,从给定范围内的数组中提取索引组列表的更有效方法

当点击tkinter菜单而不是菜单选项时,如何执行命令?

在Python中计算连续天数

在用于Python的Bokeh包中设置按钮的样式

使用Python异步地持久跟踪用户输入

为用户输入的整数查找根/幂整数对的Python练习

Pandas 数据帧中的枚举,不能在枚举列上执行GROUP BY吗?

如何在Gekko中处理跨矢量优化

如何获取包含`try`外部堆栈的`__traceback__`属性的异常

对数据帧进行分组,并按组间等概率抽样n行

正在try 让Python读取特定的CSV文件

是否需要依赖反转来确保呼叫方和被呼叫方之间的分离?

为什么在生成时间序列时,元组索引会超出范围?