Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9012205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:56:16+00:00 2026-06-16T02:56:16+00:00

I have two pandas DataFrames – weight has a simple Index on a Land

  • 0

I have two pandas DataFrames – weight has a simple Index on a Land Use columns. concentration has a MultiIndex on Land Use and Parameter.

import pandas
from io import StringIO

conc_string = StringIO("""\
Land Use,Parameter,1E,1N,1S,2
Airfield,BOD5 (mg/l),0.418,0.118,0.226,1.063
Airfield,Ortho P (mg/l),0.002,0.001,0.001,0.002
Airfield,TSS (mg/l),1.773,11.47,0.862,0.183
Airfield,Zn (mg/l),0.001,0.001,4.95E-05,0.001
"Commercial",BOD5 (mg/l),0.036,0.0419,,0.315
"Commercial",Cu (mg/l),4.37E-05,7.34E-05,,0.00039
"Commercial",O&G (mg/l),0.0385,0.127,,0.263
Open Space,TSS (mg/l),0.371,3.01,1.209,0.147
Open Space,Zn (mg/l),0.0127,0.0069,0.0132,0.007
"Parking Lot",BOD5 (mg/l),0.924,0.0668,2.603,3.19
"Parking Lot",O&G (mg/l),1.02,0.149,1.347,1.88
"Rooftops",BOD5 (mg/l),0.135,1.00,0.0562,0.310""")

weight_string = StringIO("""\
Land Use,1E,1N,1S,2
Airfield,0.511,0.0227,0.0616,0.394
Commercial,0.0005,0.1704,0,0.1065
Open Space,0.0008,0.005,0.0002,0.0004
"Parking Lot",0.33,0.514,0.252,0.171
Rooftops,0.081,0.028,8.50E-05,0.003""")

concentration = pandas.read_csv(conc_string, index_col=[0,1])
weight = pandas.read_csv(weight_string, index_col=0)

In this case, the columns (1E, 1N, 1S, and 2) are drainage basins.

What I would like to do is divide all of the concentrations independent of Parameter by the weights where the basin (column names) and Land Use.

I’m not having much luck here. concentration / weight certainly does’t work. I’m not having much luck stacking the dataframes and joining either

wstk = pandas.DataFrame(weight.stack())
wstk.index.names = ['Land Use', 'Basin']
wstk.rename(columns={0:'weight'}, inplace=True)

cstk = pandas.DataFrame(concentration.stack())
cstk.index.names = ['Land Use', 'Parameter', 'Basin']
cstk.rename(columns={0:'concentration'}, inplace=True)

wstk.join(cstk, on=['Land Use', 'Basin']) # fails 
cstk.join(wstk, on=['Land Use', 'Basin']) # fails 

The last two lines don’t raise an error when I leave off the on kwarg, but return NaN results for the joined column. They also fail if I drop the index on both stacked DataFrames (e.g., do wstk.reset_index(inplace=True) before the join).

Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:56:17+00:00Added an answer on June 16, 2026 at 2:56 am

    Use the DataFrame div method and pass matchkey for the multi-index you want to broadcast across:

    From the documentation for div:

    level : int or name
        Broadcast across a level, matching Index values on the
        passed MultiIndex level
    
    In [39]: concentration.div(weight, level='Land Use')
    Out[39]:
                                        1E          1N           1S           2
    Land Use    Parameter
    Airfield    BOD5 (mg/l)       0.818004    5.198238     3.668831    2.697970
                Ortho P (mg/l)    0.003914    0.044053     0.016234    0.005076
                TSS (mg/l)        3.469667  505.286344    13.993506    0.464467
                Zn (mg/l)         0.001957    0.044053     0.000804    0.002538
    Commercial  BOD5 (mg/l)      72.000000    0.245892          NaN    2.957746
                Cu (mg/l)         0.087400    0.000431          NaN    0.003662
                O&G (mg/l)       77.000000    0.745305          NaN    2.469484
    Open Space  TSS (mg/l)      463.750000  602.000000  6045.000000  367.500000
                Zn (mg/l)        15.875000    1.380000    66.000000   17.500000
    Parking Lot BOD5 (mg/l)       2.800000    0.129961    10.329365   18.654971
                O&G (mg/l)        3.090909    0.289883     5.345238   10.994152
    Rooftops    BOD5 (mg/l)       1.666667   35.714286   661.176471  103.333333
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two pandas dataframes one called orders and another one called daily_prices .
Assume I have a pandas DataFrame with two columns, A and B. I'd like
I have two pandas dataframes: from pandas import DataFrame df1 = DataFrame({'col1':[1,2],'col2':[3,4]}) df2 =
I'm using Pandas (0.9.1) to write a physics code. I have two dataframes: Levels:
I have two columns say Main and Sub . (they can be of same
I have two tables, lets say table1 and table2 with common columns, id and
I have two columns, senderUserID and recieverUserID in Messages table. I select senderUserID and
Suppose I have two DataFrames a & b where a is larger than b
I have two largish (snippets provided) pandas DateFrame s with unequal dates as indexes
This is my first time trying Pandas. I think I have a reasonable use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.