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

1K
Views
Usando pd.concat en lugar de df.append con pandas 1.4

Estoy usando df.append() en mi código para agregar el cambio porcentual en las columnas del marco de datos. Con df.append() depreciado en pandas 1.4, estoy tratando de usar pd.concat pero no puedo replicar la salida.

Así que esto es lo que tengo ahora:

 import numpy as np import pandas as pd df = pd.DataFrame({"A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"], "B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"], "C": ["small", "large", "large", "small", "small", "large", "small", "small", "large"], "D": [1, 2, 2, 3, 3, 4, 5, 6, 7], "E": [2, 4, 5, 5, 6, 6, 8, 9, 9]}) table = pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C'], aggfunc=np.sum, fill_value=0, margins=True) table.append( (table .iloc[-1] .pct_change(periods=1, fill_method=None) .fillna('') .apply(lambda x: '{:.1%}'.format(x) if x else '') ) )

La salida es:

 C large small All AB bar one 4 5 9 two 7 6 13 foo one 4 1 5 two 0 6 6 All 15 18 33 20.0% 83.3%

que es lo que busco, pero recibo la advertencia despreciada,

 FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.table.append()

Cambié mi código para usar pd.concat() , de la siguiente manera:

 pd.concat([table, (table .iloc[-1] .pct_change(periods=1, fill_method=None) .fillna('') .apply(lambda x: '{:.1%}'.format(x) if x else '') )], )

ahora estoy recibiendo:

 large small All 0 (bar, one) 4.0 5.0 9.0 NaN (bar, two) 7.0 6.0 13.0 NaN (foo, one) 4.0 1.0 5.0 NaN (foo, two) 0.0 6.0 6.0 NaN (All, ) 15.0 18.0 33.0 NaN large NaN NaN NaN small NaN NaN NaN 20.0% All NaN NaN NaN 83.3%

que no es lo que esperaba: tenga en cuenta los cambios porcentuales (20% y 83.3%) en comparación con la salida de append. Cualquier entrada sería apreciada.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Usa pd.concat así. Convierta su comando interno a df usando Series.to_frame y luego transpóngalo usando df.T :

 In [74]: pd.concat([table, table.iloc[-1].pct_change(periods=1, fill_method=None).fillna('').apply(lambda x: '{:.1%}'.format(x) if x else '').to_frame().T]) Out[74]: C large small All AB bar one 4 5 9 two 7 6 13 foo one 4 1 5 two 0 6 6 All 15 18 33 20.0% 83.3%
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!