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

403
Views
valores separados por comas en columnas como filas en pandas

Tengo un marco de datos en pandas como se menciona a continuación, donde los elementos en la info de la columna son los mismos que el archivo único en la id de la columna:

 id text info 1 great boy,police 1 excellent boy,police 2 nice girl,mother,teacher 2 good girl,mother,teacher 2 bad girl,mother,teacher 3 awesome grandmother 4 superb grandson

Todo lo que quiero obtener elementos de la lista como fila para cada archivo, como:

 id text info 1 great boy 1 excellent police 2 nice girl 2 good mother 2 bad teacher 3 awesome grandmother 4 superb grandson
over 4 years ago · Santiago Trujillo
5 answers
Answer question

0

Una forma de usar pandas.DataFrame.groupby.transform .

Tenga en cuenta que esto supone:

  1. los elementos en info tienen la misma longitud que el número de miembros para cada id después de dividirlos por ','
  2. los elementos en info son idénticos entre la misma identificación.

 df["info"] = df.groupby("id")["info"].transform(lambda x: x.str.split(",").iloc[0]) print(df)

Producción:

 id text info 0 1 great boy 1 1 excellent police 2 2 nice girl 3 2 good mother 4 2 bad teacher 5 3 awesome grandmother 6 4 superb grandson
over 4 years ago · Santiago Trujillo Report

0

cree una variable temporal contando el número de filas para cada grupo de info :

 temp = df.groupby('info').cumcount()

Haga una lista de comprensión para indexar por texto en info :

 df['info'] = [ent.split(',')[pos] for ent, pos in zip(df['info'], temp)] df id text info 0 1 great boy 1 1 excellent police 2 2 nice girl 3 2 good mother 4 2 bad teacher 5 3 awesome grandmother 6 4 superb grandson
over 4 years ago · Santiago Trujillo Report

0

O intente apply :

 df['info'] = pd.DataFrame({'info': df['info'].str.split(','), 'n': df.groupby('id').cumcount()}).apply(lambda x: x['info'][x['n']], axis=1)

Producción:

 >>> df id text info 0 1 great boy 1 1 excellent police 2 2 nice girl 3 2 good mother 4 2 bad teacher 5 3 awesome grandmother 6 4 superb grandson >>>
over 4 years ago · Santiago Trujillo Report

0

Intentemos

 df['new'] = df.loc[~df.id.duplicated(),'info'].str.split(',').explode().values df id text info new 0 1 great boy,police boy 1 1 excellent boy,police police 2 2 nice girl,mother,teacher girl 3 2 good girl,mother,teacher mother 4 2 bad girl,mother,teacher teacher 5 3 awesome grandmother grandmother 6 4 superb grandson grandson
over 4 years ago · Santiago Trujillo Report

0

Aproveche el hecho de que 'info' está duplicada.

 df['info'] = df['info'].drop_duplicates().str.split(',').explode().to_numpy()

Producción:

 id text info 0 1 great boy 1 1 excellent police 2 2 nice girl 3 2 good mother 4 2 bad teacher 5 3 awesome grandmother 6 4 superb grandson
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!