1

I have a pandas DataFrame with columns "Quantity" and "Unit", e.g.:

    Quantity    Unit
0   Current     A
1   Current     mA
2   Voltage     V
3   Length      m
4   Length      km

and I have a pandas DataFrame with ratios of units, e.g.:

      Current  Voltage  Length           
A       1        NaN      NaN
mA     1000      NaN      NaN
V      NaN       1        NaN
m      NaN       NaN      1000
km     NaN       NaN       1 

Is it possible to create in first dataframe column with values of ratios?:

    Quantity    Unit   Ratio
0   Current     A        1
1   Current     mA      1000
2   Voltage     V        1
3   Length      m        1
4   Length      km      1000

I have tried to get every value by iterrows, but I have really big dataframe and it's not working correctly in case of time. Maybe there is another, more expedient way to solve my issue, but I have to work with dataframes of ratios.

4 Answers 4

3

Pandas' merge() efficiently works as mapping data from one df to another

# stack the second df and merge it with the first on Unit and Quantity
df1.merge(df2.stack().rename('Ratio'), left_on=['Unit', 'Quantity'], right_index=True)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

2

Using to_dict:

d = df2.to_dict('i')

df['Ratio'] = df.apply(lambda s: d[s['Unit']][s['Quantity']], axis=1)

  Quantity Unit   Ratio
0  Current    A     1.0
1  Current   mA  1000.0
2  Voltage    V     1.0
3   Length    m  1000.0
4   Length   km     1.0

Comments

1

Given the input dataframe

df
      Current  Voltage  Length           
A       1        NaN      NaN
mA     1000      NaN      NaN
V      NaN       1        NaN
m      NaN       NaN      1000
km     NaN       NaN       1 

Then it seems like you want something as simple as

df.stack()

A   Current       1.0
mA  Current    1000.0
V   Voltage       1.0
m   Length     1000.0
km  Length        1.0
dtype: float64

Comments

0

You can use map:

df.assign(Ratio = df.Unit.map(ratios.bfill(axis = 1).Current))

  Quantity Unit   Ratio
0  Current    A     1.0
1  Current   mA  1000.0
2  Voltage    V     1.0
3   Length    m  1000.0
4   Length   km     1.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.