samman
samman

Reputation: 613

How to divide a dataframe by a scalar value obtained from another dataframe using pandas

I have a quick simple question. I have a dataframe such as this

  Column_4 Column_4
0     Data     Data
1   102832    79639
2    50034    21058
3    38230    18963
4    34503    14216

What I want to do is divide the values in each column by a scalar value that is derived from another dataframe, such as this.

          0         1
0  103000.0     500.0

So this is what I want to happen:

    Column_4           Column_4
0     Data               Data
1   102832/103000.0     79639/500.0
2    50034/103000.0     21058/500.0
3    38230/103000.0     18963/500.0
4    34503/103000.0     14216/500.0

I know you can divide a dataframe by scalar values, and even dataframes by values in other dataframes (by using the index to match them); however I have yet to find a way to divide a column of a dataframe by a scalar value that is obtained from another dataframe using pandas such as the above example.

Edit: There was a string in my column (Data), so I have removed that. This is the new dataframe.

  Column_4 Column_4 
1   102832   79639   
2    50034   21058    
3    38230   18963   
4    34503   14216     
5    34219   11450    

Upvotes: 1

Views: 604

Answers (4)

Henry Yik
Henry Yik

Reputation: 22503

If there is only one row in your second df:

df1 = pd.DataFrame({"col1":["data",100,200,300,400],
                    "col2":["data",200,300,400,500]})

df2 = pd.DataFrame({"col1":[100],
                    "col2":[200]})

print (df1.iloc[1:].astype(int)/df2.values)

   col1  col2
0   1.0   1.0
1   2.0   1.5
2   3.0   2.0
3   4.0   2.5

Upvotes: 1

oppressionslayer
oppressionslayer

Reputation: 7224

With you data set, you need to do it like this:

df4['Column_4'][1:].astype(int) / df5['0'][0]                                                                                                                                        
Out[363]: 
1    0.998369
2    0.485767
3    0.371165
4    0.334981

Upvotes: 1

cetres
cetres

Reputation: 56

Try this:

import pandas as pd
import numpy as np

df1 = pd.DataFrame([[102832, 79639], [50034, 21058], [38230, 18963], [34503, 14216]], columns=['A', 'B'])
df1

    A       B
0   102832  79639
1   50034   21058
2   38230   18963
3   34503   14216

df2 = pd.DataFrame([[103000.0, 500.0]])
df2

           0        1
0   103000.0    500.0

Columns names need to be the same from both tables

df1 / pd.DataFrame(np.repeat(df2.values, df1.shape[0], axis=0), columns=['A', 'B'])

    A           B
0   0.998369    159.278
1   0.485767    42.116
2   0.371165    37.926
3   0.334981    28.432

Upvotes: 1

9mat
9mat

Reputation: 1234

You can convert the second data frame to list and do normal division (pandas will broadcast the division to each column)

# test data
import pandas as pd
df1 = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
df2 = pd.DataFrame({'c':[7], 'd':[8]})

#df1
#   a  b
#0  1  4
#1  2  5
#2  3  6

#df2
#   c  d
#0  7  8

res = df1[['a','b']]/df2.loc[0,['c','d']].tolist()

print(res)

#          a      b
#0  0.142857  0.500
#1  0.285714  0.625
#2  0.428571  0.750

Upvotes: 1

Related Questions