loc in for-loop

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
tobentobentoben
User
Beiträge: 4
Registriert: Samstag 20. Januar 2018, 17:34

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
Sirius3
User
Beiträge: 17741
Registriert: Sonntag 21. Oktober 2012, 17:20

@tobentobentoben: please remove all these unnecessary parens, that the code is a little bit more readable. How do you know, this is not working?
tobentobentoben
User
Beiträge: 4
Registriert: Samstag 20. Januar 2018, 17:34

i tried the code several times and tried some things but its just not working.
tobentobentoben
User
Beiträge: 4
Registriert: Samstag 20. Januar 2018, 17:34

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
Benutzeravatar
noisefloor
User
Beiträge: 3856
Registriert: Mittwoch 17. Oktober 2007, 21:40
Wohnort: WW
Kontaktdaten:

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
Benutzeravatar
kbr
User
Beiträge: 1487
Registriert: Mittwoch 15. Oktober 2008, 09:27

@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)
Antworten