<p>试试这个:</p>
<pre><code>import pandas as pd
data = {'text': ['I am A. I am 30 years old. I live in NY.','I am B. I am 25 years old. I live in SD.','I am C. I am 30 years old. I live in TX.'], 'todel': [[1, 2], [1], [1, 2]]}
df = pd.DataFrame(data)
def fun(sen, lst):
return ('.'.join(s for idx, s in enumerate(sen.split('.')) if idx+1 not in lst))
df['text'] = df.apply(lambda row : fun(row['text'],row['todel']), axis=1)
</code></pre>
<p>输出:</p>
<pre><code> text todel
0 I live in NY. [1, 2]
1 I am 25 years old. I live in SD. [1]
2 I live in TX. [1, 2]
</code></pre>
<p><strong>根据已编辑的问题进行编辑:</strong></p>
<p>如果<code>df['text']</code>列出了不需要拆分的句子,您可以尝试以下方法:</p>
<pre><code>data = {'text': [['I am A.', 'I am 30 years old.', 'I live in NY.'],
['I am B.', 'I am 25 years old.', 'I live in SD.'],
['I am C.','I am 30 years old.',' I live in TX.']], 'todel': [[1, 2], [1], [1, 2]]}
df = pd.DataFrame(data)
# text todel
# 0 [I am A., I am 30 years old., I live in NY.] [1, 2]
# 1 [I am B., I am 25 years old., I live in SD.] [1]
# 2 [I am C., I am 30 years old., I live in TX.] [1, 2]
def fun(sen, lst):
return [s for idx , s in enumerate(sen) if not idx+1 in lst]
df['text'] = df.apply(lambda row : fun(row['text'],row['todel']), axis=1)
print(df)
</code></pre>
<p>输出:</p>
<pre><code> text todel
0 [I live in NY.] [1, 2]
1 [I am 25 years old., I live in SD.] [1]
2 [ I live in TX.] [1, 2]
</code></pre>