0

Hello I´m trying to get the previous value in an specific column if this valuecontains "-":

this is my code:

count=-1
for i, row in df1.iterrows():
    count=count + 1
    if row["SUBCAPITULO"]== " "and count>0 and "-" in df1.loc[count-1:"SUBCAPITULO"]:
        row["SUBCAPITULO"]= df1.loc[count-1:"SUBCAPITULO"]

enter image description here

not_speshal
  • 22,093
  • 2
  • 15
  • 30

3 Answers3

0

Use shift:

Sample:

>>> df
  SUBCAPITULO
0       dash-
1
2      comma,
3
4        dot.
df.loc[(df['SUBCAPITULO'] == ' ') &
       (df['SUBCAPITULO'].shift().str.contains('-'))] = df['SUBCAPITULO'].shift()
>>> df
  SUBCAPITULO
0       dash-
1       dash-
2      comma,
3
4        dot.
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Desired output has not been posted and the request is unclear.

Taking these comments into account:

  • trying to get the previous row value in an specific column if this value contains a dash
  • I think OP wants to fill the dash- value over multiple lines

Maybe this helps...

import pandas as pd

df = pd.read_csv('test.csv')

print(df, '\n\n')

'''
Shows:

  Other_data       SUBCAPITULO
0       qwer               NaN
1       vfds               NaN
2       sdfg  1.01 – TORRE – 1
3       hfgt               NaN
4       jkiu          capitulo
5       bvcd  2.01 – TORRE – 1
6       grnc               NaN
7       sdfg          capitulo
8       poij               NaN
9       fghg  2.01 – TORRE – 1 

'''

for i in reversed(df.index):
    if i >= 1:
        if '–' in str(df.loc[i, 'SUBCAPITULO']):
            if str(df.loc[i-1, 'SUBCAPITULO']) == 'nan': 
                df.loc[i-1, 'SUBCAPITULO'] = df.loc[i, 'SUBCAPITULO']

print(df)


'''
Shows:

  Other_data       SUBCAPITULO
0       qwer  1.01 – TORRE – 1
1       vfds  1.01 – TORRE – 1
2       sdfg  1.01 – TORRE – 1
3       hfgt               NaN
4       jkiu          capitulo
5       bvcd  2.01 – TORRE – 1
6       grnc               NaN
7       sdfg          capitulo
8       poij  2.01 – TORRE – 1
9       fghg  2.01 – TORRE – 1

'''

print('\n')
MDR
  • 2,610
  • 1
  • 8
  • 18
0
df1.reset_index(drop=True, inplace=True)
for i in range(1, len(df1)):
  if df1.loc[i, 'SUBCAPITULO'] == " " and "-" in df1.loc[i-1, 'SUBCAPITULO']:
    df1.loc[i, 'SUBCAPITULO']=df1.loc[i-1, 'SUBCAPITULO']

df1.dropna(inplace=True)

The problem was that I had TO RESET INDEX before the loop.

enter image description here