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

252
Views
¿Cómo explotar dinámicamente usando la columna de pandas?

Tengo un marco de datos que se ve así

 import pandas as pd import numpy as np # Create data set. dataSet = {'id': ['A', 'A', 'B'], 'id_2': [1, 2, 1] , 'number': [320, 169, 120], 'add_number' : [4,6,3]} # Create dataframe with data set and named columns. df = pd.DataFrame(dataSet, columns= ['id', 'id_2','number', 'add_number']) id id_2 number add_number 0 A 1 320 4 1 A 2 169 6 2 B 1 120 3

Me gustaría usar number y add_number para poder explotar esto dinámicamente, es decir, 320 + 4 tendría [320,321,322,323,324] (hasta 324, y me gustaría explotar en esto)

SALIDA DESEADA

 id id_2 number 0 A 1 320 1 A 1 321 2 A 1 322 3 A 1 323 4 A 1 324 5 A 2 169 6 A 2 170 7 A 2 171 8 A 2 172 9 A 2 173 10 A 2 174 11 A 2 175 12 B 1 120 13 B 1 121 14 B 1 122 15 B 1 123

Miré por encima de la función de pandas de explosión, ancho_a_largo, pero no sé por dónde empezar, ¡cualquier sentido de la dirección sería apreciado!

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

En su caso, repeat y luego groupby cumcount agregue el valor al número

 out = df.reindex(df.index.repeat(df.add_number+1)) out['number'] = out['number'].add(out.groupby(out.index).cumcount()) out Out[22]: id id_2 number add_number 0 A 1 320 4 0 A 1 321 4 0 A 1 322 4 0 A 1 323 4 0 A 1 324 4 1 A 2 169 6 1 A 2 170 6 1 A 2 171 6 1 A 2 172 6 1 A 2 173 6 1 A 2 174 6 1 A 2 175 6 2 B 1 120 3 2 B 1 121 3 2 B 1 122 3 2 B 1 123 3
over 4 years ago · Santiago Trujillo Report

0

Aquí hay un enfoque alternativo, en caso de que quieras usar explotar específicamente:

 import pandas as pd import numpy as np # Create data set. dataSet = {'id': ['A', 'A', 'B'], 'id_2': [1, 2, 1] , 'number': [320, 169, 120], 'add_number' : [4,6,3]} df = pd.DataFrame(dataSet, columns= ['id', 'id_2','number', 'add_number']) new_numbers = [] for row in df.iterrows(): new_numbers.append([row[1]['number']+ i for i in range(row[1]['add_number']+1)]) df['number'] = new_numbers df = df.drop(['add_number'], axis=1) df = df.explode('number').reset_index().drop(['index'], axis=1) print(df)

Producción:

 id id_2 number 0 A 1 320 1 A 1 321 2 A 1 322 3 A 1 323 4 A 1 324 5 A 2 169 6 A 2 170 7 A 2 171 8 A 2 172 9 A 2 173 10 A 2 174 11 A 2 175 12 B 1 120 13 B 1 121 14 B 1 122 15 B 1 123
over 4 years ago · Santiago Trujillo Report

0

Multiplicar columnas basadas en la columna de number od

 new=pd.DataFrame(np.repeat(df.values,df['add_number']+1, axis=0), columns=df.columns)

#Use groupby cuente acumulativamente las filas añadidas e incremente el número por el mismo

 new=new.assign(number=new['number']+new.groupby(['number','id'])['number'].transform('cumcount'))

Salir

 id id_2 number add_number 0 A 1 320 4 1 A 1 321 4 2 A 1 322 4 3 A 1 323 4 4 A 1 324 4 5 A 2 169 6 6 A 2 170 6 7 A 2 171 6 8 A 2 172 6 9 A 2 173 6 10 A 2 174 6 11 A 2 175 6 12 B 1 120 3 13 B 1 121 3 14 B 1 122 3 15 B 1 123 3
over 4 years ago · Santiago Trujillo Report

0

Intenta usar np.arange y explode :

 df['range'] = df.apply(lambda x: np.arange(x['number'], x['number']+x['add_number']+1), axis=1) df.explode('range')

o

 df['range'] = [np.arange(n, n+a+1) for n, a in zip(df['number'],df['add_number'])] df.explode('range')

Producción:

 id id_2 number add_number range 0 A 1 320 4 320 0 A 1 320 4 321 0 A 1 320 4 322 0 A 1 320 4 323 0 A 1 320 4 324 1 A 2 169 6 169 1 A 2 169 6 170 1 A 2 169 6 171 1 A 2 169 6 172 1 A 2 169 6 173 1 A 2 169 6 174 1 A 2 169 6 175 2 B 1 120 3 120 2 B 1 120 3 121 2 B 1 120 3 122 2 B 1 120 3 123
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!