• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

212
Views
Cómo expandir y crear el siguiente conjunto de datos en Pandas

Tengo un conjunto de datos que se ve así:

ingrese la descripción de la imagen aquí

 df = pd.DataFrame({ 'weekstart':['01-Jan-18','08-Jan-18','15-Jan-18','22-Jan-18'], 'weekend':['07-Jan-18','14-Jan-18','21-Jan-18','28-Jan-18'], 'Spend':[34235.922,37359.6048,38916.1164,36903.8628], 'Daily':[4890.846,5337.086,5559.445,5271.98], })

Me gustaría expandir esto para tomar el valor diario y crear un nuevo conjunto de datos con fechas diarias para formar algo como esto:

ingrese la descripción de la imagen aquí

¡¡Gracias!!

over 3 years ago · Santiago Trujillo
2 answers
Answer question

0

  1. Convertir a fecha y hora
  2. Use pd.date_range para crear una columna de todas las fechas entre "weekstart" y "weekend"
  3. Use explode para dividir en filas individuales.
 df["weekstart"] = pd.to_datetime(df["weekstart"]) df["weekend"] = pd.to_datetime(df["weekend"]) df["days"] = df.apply(lambda x: pd.date_range(x["weekstart"], x["weekend"], freq="D"), axis=1) df = df.explode("days") output = df[["days", "Daily"]] >>> output days Daily 0 2018-01-01 4890.846 0 2018-01-02 4890.846 0 2018-01-03 4890.846 0 2018-01-04 4890.846 0 2018-01-05 4890.846 0 2018-01-06 4890.846 0 2018-01-07 4890.846 1 2018-01-08 5337.086 1 2018-01-09 5337.086 1 2018-01-10 5337.086 1 2018-01-11 5337.086 1 2018-01-12 5337.086 1 2018-01-13 5337.086 1 2018-01-14 5337.086 2 2018-01-15 5559.445 2 2018-01-16 5559.445 2 2018-01-17 5559.445 2 2018-01-18 5559.445 2 2018-01-19 5559.445 2 2018-01-20 5559.445 2 2018-01-21 5559.445 3 2018-01-22 5271.980 3 2018-01-23 5271.980 3 2018-01-24 5271.980 3 2018-01-25 5271.980 3 2018-01-26 5271.980 3 2018-01-27 5271.980 3 2018-01-28 5271.980
over 3 years ago · Santiago Trujillo Report

0

Parece un problema fácil. Repita el marco de datos existente usando df.iterrows() y luego obtenga el rango de fechas actual, luego use este rango para iterar un rango de fechas y use estas fechas para completar otro marco de datos con las fechas y cantidades detalladas. Reitero el marco de datos porque estoy expandiendo los datos en un nuevo marco de datos en lugar de agregar una nueva columna usando una explosión de marco de datos

 df = pd.DataFrame({ 'weekstart':['01-Jan-18','08-Jan-18','15-Jan-18','22-Jan-18'], 'weekend':['07-Jan-18','14-Jan-18','21-Jan-18','28-Jan-18'], 'Spend':[34235.922,37359.6048,38916.1164,36903.8628], 'Daily':[4890.846,5337.086,5559.445,5271.98], }) #for key, value in df.iterrows(): # fill a range with weekstart and weekend date ranges: # iterate the range and populate the new data frame df2=pd.DataFrame(columns=['Date','Amount']) df2['Date']=pd.to_datetime(df2['Date']) for key,value in df.iterrows(): amount=value['Daily'] for date in pd.date_range(value['weekstart'], value['weekend']): df2=df2.append({"Date":date, "Amount":amount},ignore_index=True) index=range(1,len(df2)+1) df2.set_index(pd.Index(index),'index',inplace=True) plt.plot(df2['Date'],df2['Amount']) plt.xticks(rotation=90) plt.show() print(df2)

producción:

 Date Amount 1 2018-01-01 4890.846 2 2018-01-02 4890.846 3 2018-01-03 4890.846 4 2018-01-04 4890.846 5 2018-01-05 4890.846 6 2018-01-06 4890.846 7 2018-01-07 4890.846 8 2018-01-08 5337.086 9 2018-01-09 5337.086 10 2018-01-10 5337.086 11 2018-01-11 5337.086 12 2018-01-12 5337.086 13 2018-01-13 5337.086 14 2018-01-14 5337.086 15 2018-01-15 5559.445 16 2018-01-16 5559.445 17 2018-01-17 5559.445 18 2018-01-18 5559.445 19 2018-01-19 5559.445 20 2018-01-20 5559.445 21 2018-01-21 5559.445 22 2018-01-22 5271.980 23 2018-01-23 5271.980 24 2018-01-24 5271.980 25 2018-01-25 5271.980 26 2018-01-26 5271.980 27 2018-01-27 5271.980 28 2018-01-28 5271.980
over 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error