我有一个数据框,其中有一个名为
section
的字段。有些行的值为“Concurrent Opinion - x”,其中 x 是相对于当前文档的索引,当前文档是数据帧中的另一个字段。我需要将同一文档中位于“Concurrent Opinion - x”下方(之后)和“Concurrent Opinion - x+1”之前的所有行的 section
字段的值设置为“Concurrent Opinion - x” .
我可以获取整个数据帧与“Concurrent Opinion”匹配的行,但我似乎无法将之间的行设置为所需的值:当我尝试设置匹配行的部分值时,代码不会不停止,但不知何故,它没有挂起。我做错了什么?
这是我的代码
concurrent_opinions_ids=paragraphs_df[paragraphs_df['section'].str.contains("Concurrent Opinion")].index
for index in concurrent_opinions_ids:
#get document_pos for paragraph
curr_pos=paragraphs_df.loc[index, 'document_pos'].copy()
curr_doc=paragraphs_df.loc[index, 'document'].copy()
curr_section=paragraphs_df.loc[index, 'section']
paragraphs_indices=paragraphs_df[(paragraphs_df['document']==curr_doc) & (paragraphs_df['document_pos']>curr_pos)].index
for ind in paragraphs_indices:
#set section to current section
paragraphs_df.loc[ind, 'section']=curr_section
最后一行,
paragraphs_df.loc[ind, 'section']=curr_section
给我带来了麻烦。
我还尝试
curr_section=paragraphs_df.loc[index, 'section'].copy()
获取该值,认为该问题可能是导致某种引用问题的原因,但它给了我一个错误,指出 'str' 对象没有属性 'copy'
更改前的数据框:
id document paragraph_id previous next length document_pos \
4001 4001 16 187 186 188 35 55282
4002 4002 16 188 187 189 4 55317
4003 4003 16 189 188 190 86 55321
4004 4004 16 190 189 191 230 55407
4005 4005 16 191 190 192 1702 55637
4006 4006 16 192 191 193 154 57339
4007 4007 16 193 192 194 49 57493
4008 4008 16 194 193 195 720 57542
4009 4009 16 195 194 196 4 58262
4010 4010 16 196 195 197 232 58266
4011 4011 16 197 196 198 297 58498
4012 4012 16 198 197 199 36 58795
4013 4013 16 199 198 200 206 58831
4014 4014 16 200 199 201 42 59037
section function \
4001 Decision Pending
4002 Decision Pending
4003 Concurrent Opinion - 1 Pending
4004 Decision Pending
4005 Decision Pending
4006 Decision Pending
4007 Decision Pending
4008 Decision Pending
4009 Decision Pending
4010 Decision Pending
4011 Decision Pending
4012 Decision Pending
4013 Decision Pending
4014 Decision Pending
text
4001 NILSON PINILLA PINILLA Magistrado
4002 25
4003 SALVAMENTO DE VOTO DEL MAGISTRADO JORGE IVÁN ...
4004 PRINCIPIO DE OFICIOSIDAD Y DE PREVALENCIA DEL...
4005 Considero, sin embargo, que la argumentación ...
4006 FACULTAD OFICIOSA DEL JUEZ ADMINISTRATIVO-Jue...
4007 DERECHO PROCESAL-Finalidad (Salvamento de voto)
4008 La finalidad principal del derecho procesal y...
4009 27
4010 DERECHO DE ACCESO A LA ADMINISTRACION DE JUST...
4011 CONSTITUCIONALIZACION DEL DERECHO PROCESAL- O...
4012 Referencia: expediente T-3.407.509
4013 Acción de tutela instaurada por Tito Edmundo ...
4014 Magistrado Ponente: ALEXEI JULIO ESTRADA
这是更改后数据框的样子:
id document paragraph_id previous next length document_pos \
4001 4001 16 187 186 188 35 55282
4002 4002 16 188 187 189 4 55317
4003 4003 16 189 188 190 86 55321
4004 4004 16 190 189 191 230 55407
4005 4005 16 191 190 192 1702 55637
4006 4006 16 192 191 193 154 57339
4007 4007 16 193 192 194 49 57493
4008 4008 16 194 193 195 720 57542
4009 4009 16 195 194 196 4 58262
4010 4010 16 196 195 197 232 58266
4011 4011 16 197 196 198 297 58498
4012 4012 16 198 197 199 36 58795
4013 4013 16 199 198 200 206 58831
4014 4014 16 200 199 201 42 59037
section function \
4001 Decision Pending
4002 Decision Pending
4003 Concurrent Opinion - 1 Pending
4004 Concurrent Opinion - 1 Pending
4005 Concurrent Opinion - 1 Pending
4006 Concurrent Opinion - 1 Pending
4007 Concurrent Opinion - 1 Pending
4008 Concurrent Opinion - 1 Pending
4009 Concurrent Opinion - 1 Pending
4010 Concurrent Opinion - 1 Pending
4011 Concurrent Opinion - 1 Pending
4012 Concurrent Opinion - 1 Pending
4013 Concurrent Opinion - 1 Pending
4014 Concurrent Opinion - 1 Pending
text
4001 NILSON PINILLA PINILLA Magistrado
4002 25
4003 SALVAMENTO DE VOTO DEL MAGISTRADO JORGE IVÁN ...
4004 PRINCIPIO DE OFICIOSIDAD Y DE PREVALENCIA DEL...
4005 Considero, sin embargo, que la argumentación ...
4006 FACULTAD OFICIOSA DEL JUEZ ADMINISTRATIVO-Jue...
4007 DERECHO PROCESAL-Finalidad (Salvamento de voto)
4008 La finalidad principal del derecho procesal y...
4009 27
4010 DERECHO DE ACCESO A LA ADMINISTRACION DE JUST...
4011 CONSTITUCIONALIZACION DEL DERECHO PROCESAL- O...
4012 Referencia: expediente T-3.407.509
4013 Acción de tutela instaurada por Tito Edmundo ...
4014 Magistrado Ponente: ALEXEI JULIO ESTRADA
您可以通过将所有
Decision
值替换为 NaN
,向前填充,然后再次用 NaN
替换任何剩余的 Decision
值(以修复第一个 Concurrent Opinion
之前的值)来实现您想要的目的。使用此示例数据的示例:
data = {'section': ['Decision', 'Decision', 'Concurrent Opinion - 1', 'Decision', 'Decision', 'Decision', 'Decision', 'Decision', 'Decision', 'Decision', 'Decision', 'Concurrent Opinion - 2', 'Decision', 'Decision']}
df = pd.DataFrame(data)
df['section'] = df['section'].replace({ 'Decision' : np.nan }).ffill().fillna('Decision')
输出:
section
0 Decision
1 Decision
2 Concurrent Opinion - 1
3 Concurrent Opinion - 1
4 Concurrent Opinion - 1
5 Concurrent Opinion - 1
6 Concurrent Opinion - 1
7 Concurrent Opinion - 1
8 Concurrent Opinion - 1
9 Concurrent Opinion - 1
10 Concurrent Opinion - 1
11 Concurrent Opinion - 2
12 Concurrent Opinion - 2
13 Concurrent Opinion - 2