BUILDING UNITS
0 01 ONE FAMILY DWELLINGS 168.0
1 02 TWO FAMILY DWELLINGS 269.0
2 03 THREE FAMILY DWELLINGS 234.0
3 04 TAX CLASS 1 CONDOS 0.0
4 06 TAX CLASS 1 - OTHER 0.0
5 07 RENTALS - WALKUP APARTMENTS 10232.0
6 08 RENTALS - ELEVATOR APARTMENTS 15136.0
7 09 COOPS - WALKUP APARTMENTS 0.0
8 10 COOPS - ELEVATOR APARTMENTS 0.0
9 11A CONDO-RENTALS 0.0
10 12 CONDOS - WALKUP APARTMENTS 0.0
11 13 CONDOS - ELEVATOR APARTMENTS 0.0
12 14 RENTALS - 4-10 UNIT 534.0
13 15 CONDOS - 2-10 UNIT RESIDENTIAL 0.0
I want to be able to group things that contain a string. For example, here, I'd want to group all the condos example into one category with a a sum of the units, the same, rentals would be grouped as rentals. The remaining ones that don't fall into any related category would be grouped as other.
I know that this selects those which contain condo, but how do you group them and sum their units as one category?
df[df['BUILDING'].str.contains("CONDO")]
I'm looking for something like so:
Building Units
Other #
Condos #sum condos
Rentals #sum rentals
Use numpy.select to create a marker for building units, and use it for groupby operations
df["BUILDING UNITS"] = np.select(
condlist=[
df["BUILDING"].str.contains("CONDO"),
df["BUILDING"].str.contains("DWELLINGS"),
],
choicelist=[
"Condos", "Rentals"
],
default="Other"
)
df.groupby("BUILDING UNITS").sum()
UNITS
BUILDING UNITS
Condos 0.0
Other 25902.0
Rentals 671.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With