Seite 1 von 1

loc in for-loop

Verfasst: Samstag 20. Januar 2018, 17:36
von tobentobentoben
Hello everybody,

i want to check whether the values in a dataframe are smaller than their predecessors AND their successors. If so, the value should be overwritten.

Can anyone explain to me, why this is not working?

Code: Alles auswählen

df2=deepcopy(df1)
i=0
for i in range(10,(len(df2)-1)):
    if (df2.loc[i+1,('Klemme03Value2')])>(df2.loc[i,('Klemme03Value2')]):
        if (df2.loc[i-1,('Klemme03Value2')])>(df2.loc[i,('Klemme03Value2')]):
            (df2.loc[i,('Klemme03Value2')])=((df2.loc[i+1,('Klemme03Value2')]))
Thanks ad Regards

Tobias

Re: loc in for-loop

Verfasst: Samstag 20. Januar 2018, 18:02
von Sirius3
@tobentobentoben: please remove all these unnecessary parens, that the code is a little bit more readable. How do you know, this is not working?

Re: loc in for-loop

Verfasst: Samstag 20. Januar 2018, 18:21
von tobentobentoben
i tried the code several times and tried some things but its just not working.

Re: loc in for-loop

Verfasst: Samstag 20. Januar 2018, 20:12
von tobentobentoben
i got rid of most parentheses, should still be executable

Code: Alles auswählen

df2=deepcopy(df1)
for i in range(10,len(df2)-1):
    if (df2.loc[i+1,'Klemme03Value2'])>(df2.loc[i,'Klemme03Value2']):
        if df2.loc[i-1,'Klemme03Value2']>df2.loc[i,'Klemme03Value2']:
            df2.loc[i,'Klemme03Value2']=df2.loc[i+1,'Klemme03Value2']
thanks for any help

Re: loc in for-loop

Verfasst: Samstag 20. Januar 2018, 20:28
von noisefloor
Hallo,

@tobentobentoben: auch wenn hier viele sicherlich Englisch können - die Sprache hier ist normalerweise deutsch, nicht englisch. Es gibt auch auch englischsprachige Supportforen für Python.

"not working" ist auch keine brauchbare Fehlermeldung... Was passiert genau (oder auch nicht)? Bekommst du eine Exception? Wird nichts geändert? Werden Werte geändert, die gar nicht geändert werden sollen? ...?

Gruß, noisefloor

Re: loc in for-loop

Verfasst: Samstag 20. Januar 2018, 20:41
von kbr
@tobentobentoben: Why it's not working? What is the result you get instead of the expected one? The following simplified adaption works as it should:

Code: Alles auswählen

s = [10, 12, 13, 9, 14, 15]
for i in range(1, len(s)-1):
    if s[i+1] > s[i] < s[i-1]:
        s[i] = s[i+1]
print(s)