Cercando di riportare i valori da confrontare due colonne da due diversi set di dati

0

Domanda

Ho due kart racing set di dati con 9 colonne:

df1:

df1 dataset

df2: df2 dataset

Sto cercando di impostare più le condizioni di cui due insiemi di dati sono confrontati e cercando di stampa True o false se le condizioni sono soddisfacenti dopo il confronto. le condizioni sono:

  1. race_start (df1) <= race_start (df2)
  2. race_end (df1) >= race_end(df2)
  3. safety_start(df1) <= safety_start (df2)
  4. safety_end (df1) >= safety_end (df2)
  5. starting_front (df1) <= starting_front (df2)
  6. starting_back (df1) <= starting_back (df2)
  7. pitstop (df1) >= pitstop (df2)
  8. no_pitstop (df1) >= no_pitstop (df2)
  9. stato (df1) = stato (df2).

prima, ho provato questo:

import numpy as np
df1['race_start_final'] = np.where(df1.race_start <= df2.race_start, 'True', 'False')
df1['race_end_final'] = np.where(df1.race_end >= df2.race_end, 'True', 'False')
df1['safety_start_final'] = np.where(df1.safety_start <= df2.safety_start, 'True', 'False')
df1['safety_end_final'] = np.where(df1.safety_end >= df2.safety_end, 'True', 'False')
df1['starting_front_final'] = np.where(df1.starting_front <= df2.starting_front, 'True', 'False')
df1['starting_back_final'] = np.where(df1.starting_back <= df2.starting_back, 'True', 'False')
df1['pitstop_final'] = np.where(df1.pitstop >= df2.pitstop, 'True', 'False')
df1['pitstop_final'] = np.where(df1.pitstop >= df2.pitstop, 'True', 'False')
df1['status_final'] = np.where(df1.status == df2.status, 'True', 'False')

ma ho ricevuto un errore che dice:

ValueError: Can only compare identically-labeled Series objects

Sto facendo giusto? Ho cercato di trovare soluzioni alternative, ma non ha trovato importanti per ciò che sto cercando di fare? Qualcuno può dirmi se la procedura che ho scelto è il modo giusto? Grazie

dataframe dataset pandas python
2021-11-24 03:02:00
1

Migliore risposta

0

Questo errore si verifica quando si tenta di confrontare due panda DataFrames e l'indice di etichette o le etichette di colonna non corrispondono perfettamente o dispone di diverse dimensioni.

È possibile troncare DataFrames prima dove la funzione if-else:

df1 = pd.DataFrame([
    [10.5, 8.5],
    [8.5, 8.5]],
    columns=['race_start','race_end'])
df2 = pd.DataFrame([
    [9.8, 9.8],
    [9.8, 9.8],
    [8.5, 8.5]],
    columns=['race_start','race_end'])

if len(df1) > len(df2):
    df1=df1.tail(df2.shape[0]).reset_index()
else:
    df2=df2.tail(df1.shape[0]).reset_index()
    
df1['race_start_final'] = np.where(df1.race_start <= df2.race_start, 'True', 'False')
2021-11-25 15:32:27

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................