Python Forum
Create new dataframe from old dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create new dataframe from old dataframe
#1
I have a pandas dataframe that has 2 columns
[Image: input.png]

,And I want to create a new dataframe with column names: lowest, highest, count, and amount. The lowest and highest columns represent the range. The count column represents the number of values coming in the particular range, and the amount column represents the total of the values in the particular range.
[Image: output.png]

And is it possible that we save that dataframe as an Excel file, and we can change the lowest and highest values in excel files according to our needs, while other values automatically get calculated.
Reply
#2
Sounds like you want to bin the data (binning/bucketing). You can use cut, qcut.

You can save the dataframe to an excel table, but it is just going to be a csv data. No formulas. It will not respond to changing values.
Reply
#3
Sounds like you want to bin the data (binning/bucketing). You can use cut, qcut. -

Please walk me through it if you could.
Reply
#4
This should get you started. The key is line 7 which handles the cuts and ranges, and then sums based on that. Can use .count() instead for the count. Website is not formatting the columns quite correctly, better if you do on your own notebook.

import pandas as pd
import numpy as np

d = {"Sno":[1,2,3,4,5,6,7,8,9,10], "Amount":[451492,448612,451492,301492,451492,429492,451492,604492,451492,130424]}
df = pd.DataFrame(data=d)

df.groupby(pd.cut(df["Amount"], np.arange(0, 500000, 50000))).sum()
Output:
Sno Amount Amount (0, 50000] 0 0 (50000, 100000] 0 0 (100000, 150000] 10 130424 (150000, 200000] 0 0 (200000, 250000] 0 0 (250000, 300000] 0 0 (300000, 350000] 4 301492 (350000, 400000] 0 0 (400000, 450000] 8 878104
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 378 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Dataframe copy warning sawtooth500 4 322 Mar-25-2024, 11:38 PM
Last Post: sawtooth500
  FutureWarning: The behavior of DataFrame concatenation with empty or all-NA entries i sawtooth500 3 939 Mar-22-2024, 03:08 AM
Last Post: deanhystad
  Adding PD DataFrame column bsben 2 299 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 727 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Comparing Dataframe to String? RockBlok 2 400 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Filter data into new dataframe as main dataframe is being populated cubangt 8 994 Oct-23-2023, 12:43 AM
Last Post: cubangt
  DataFRame.concat() nafshar 3 770 Jul-14-2023, 04:41 PM
Last Post: nafshar
  Convert dataframe from str back to datafarme Creepy 1 619 Jul-07-2023, 02:13 PM
Last Post: snippsat
  Question on pandas.dataframe merging two colums shomikc 4 817 Jun-29-2023, 11:30 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020