Home > OS >  Why do I get a KeyError:7?
Why do I get a KeyError:7?

Time:08-11

I need to split the column 'Diff' in 3 different columns in order to perform a regression analysis. I'd like to get the value on a column just if it matches a condition in another column. For example, in 'Diff1' i want to put the values from 'Diff' if in the column 'REGOLA' the row has 'DESIGNATO GIUDICE FISSATA'.

Using the following code, i get the error KeyError:7. Any suggestions on how to fix it or other ways to reach the same result?

The column 'Diff' is the seventh column btw.

import pandas as pd
import re

#select only rows with:
#1. ISCRITTO RUOLO GENERALE
#2. DESIGNATO GIUDICE FISSATA
#3. SENTENZA VERBALE CON NUMERO
#4. DEPOSITATA PUBBLICATA SENTENZA CONFERMA

df_reg= alldf_semplificato[(alldf_semplificato['REGOLA']=='ISCRITTO RUOLO GENERALE') 
| (alldf_semplificato['REGOLA']=='DESIGNATO GIUDICE FISSATA')  
| (alldf_semplificato['REGOLA']=='SENTENZA VERBALE CON NUMERO')  
| (alldf_semplificato['REGOLA']=='DEPOSITATA PUBBLICATA SENTENZA CONFERMA')]

#compute the difference on index
df_reg['Diff'] = df_reg.index.to_series().diff()

#split the column 'Diff' in 3 different columns
df_reg['Diff1'] = df_reg.loc[df_reg['REGOLA'] == 'DESIGNATO GIUDICE FISSATA', 7]
df_reg['Diff2'] = df_reg.loc[df_reg['REGOLA']=='SENTENZA VERBALE CON NUMERO', 7]
df_reg['Diff3'] = df_reg.loc[df_reg['REGOLA']=='DEPOSITATA PUBBLICATA SENTENZA CONFERMA', 7]

df_reg

CodePudding user response:

df_reg['Diff1'] = df_reg.loc[df_reg['REGOLA'] == 'DESIGNATO GIUDICE FISSATA', "Diff"]

  • Related