Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

320
Views
los pandas encuentran el punto de inicio y parada de valores no nulos

Me gustaría encontrar los puntos de inicio y finalización de una columna y marcarlos como se muestra a continuación:

valor bandera
Yaya Yaya
Yaya Yaya
1 comienzo
2 Yaya
1 Yaya
3 Yaya
2 detener
Yaya Yaya
1 comienzo
2 detener
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

  • el start ocurre cuando el valor actual no es notnull y el valor anterior es isnull
  • la stop ocurre cuando el valor actual no es notnull y el siguiente valor isnull

Genere estas condiciones usando shift y asigne usando loc :

 start = df.value.notnull() & df.value.shift().isnull() stop = df.value.notnull() & df.value.shift(-1).isnull() df.loc[start, 'flag'] = 'start' df.loc[stop, 'flag'] = 'stop' # value flag # 0 NaN NaN # 1 NaN NaN # 2 1.0 start # 3 2.0 NaN # 4 1.0 NaN # 5 3.0 NaN # 6 2.0 stop # 7 NaN NaN # 8 1.0 start # 9 2.0 stop

Alternativamente, asigne usando mask :

 df['flag'] = df['flag'].mask(start, 'start') df['flag'] = df['flag'].mask(stop, 'stop')
over 4 years ago · Santiago Trujillo Report

0

Aquí recorrí las filas y usé una bandera para indicar si estábamos comenzando o no.

 start_flag = 0 for index, row in df.iterrows(): if row['val'].isnull(): df.loc[index, 'flag'] = "NaN" start_flag = 0 else: if start_flag == 0: df.loc[index, 'flag'] = "start" start_flag = 1 else: if (index < df.shape[0]-1 and df.loc[index+1, 'val'].isnull()) or index == df.shape[0]-1: df.loc[index, 'flag'] = "stop"
over 4 years ago · Santiago Trujillo Report

0

Esto es lo que necesitas:

 # Auxiliar columns to detect start and end df['Past'] = df['Value'].shift(-1) df['Future'] = df['Value'].shift(1) # Auxiliar function to complete new column def Find_start_stop_Null(row): flag = np.nan if ((not pd.isnull(row['Value'])) and (pd.isnull(row['Future']))): flag = 'start' elif ((not pd.isnull(row['Value'])) and (pd.isnull(row['Past']))): flag = 'stop' return flag df['flag'] = df.apply(lambda row: Find_start_stop_Null(row), axis=1) # Drop unnecessary columns df = df.drop('Past', axis=1) df = df.drop('Future', axis=1)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!