How to Divide Each Column of Pandas Dataframe by a Series

Python: Divide each row of a DataFrame by another DataFrame vector

In df.divide(df2, axis='index'), you need to provide the axis/row of df2 (ex. df2.iloc[0]).

import pandas as pd

data1 = {"a":[1.,3.,5.,2.],
"b":[4.,8.,3.,7.],
"c":[5.,45.,67.,34]}
data2 = {"a":[4.],
"b":[2.],
"c":[11.]}

df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

df1.div(df2.iloc[0], axis='columns')

or you can use df1/df2.values[0,:]

Dividing each element of a python dataframe to a Series

The exact output that you expect is unclear, but if I guess correctly, you might want to only divide the common indices/columns and leave the rest untouched.

For this you can use combine_first to fill the NaNs after the division:

column "a" only:

df.div(df2, axis=0).combine_first(df)

all columns:

df.div(df2['a'], axis=0).combine_first(df)

or reindex_like and fillna to align the dataframes before division.

column "a" only

df.div(df2.reindex_like(df).fillna(1), axis=0)

output (column a only):

       a    b     c
0 0.250 4.0 5.0
1 0.500 8.0 45.0
2 0.625 3.0 67.0
3 2.000 7.0 34.0

output (all columns):

       a         b       c
0 0.250 1.000000 1.250
1 0.500 1.333333 7.500
2 0.625 0.375000 8.375
3 2.000 7.000000 34.000

how do you divide each value from a pandas series in sequence

What it seems to me is that you are trying to divide a number in a given row by the one of the previous. This can be achieved using this code

import pandas as pd
import numpy as np
a = pd.Series([1, 2, 16,64,128,360,720])
division = pd.Series(np.divide(a.values[1:],a.values[:-1]))
index = pd.Series(np.multiply(division == 2, [i for i in range(len(a)-1)]))

Note: your question is very ill posed. You didn't specify what you wanted to achieve, I figured out by myself from the example. You also added a wrong snipped of code. Pay attention to make a nicer question next time

Divide columns in a DataFrame by a Series (result is only NaNs?)

The cause of NaNs are due to misalignment of your indexes. To get over this, you will either need to divide by numpy arrays,

# <=0.23
df.values / df2[['x']].values # or df2.values assuming there's only 1 column
# 0.24+
df.to_numpy() / df[['x']].to_numpy()

array([[0.09090909, 0.18181818, 0.27272727],
[0.33333333, 0.41666667, 0.5 ],
[0.53846154, 0.61538462, 0.69230769]])

Or perform an axis aligned division using .div:

df.div(df2['x'], axis=0)
a b c
0 0.090909 0.181818 0.272727
1 0.333333 0.416667 0.500000
2 0.538462 0.615385 0.692308

Dataframe divide series on pandas

Here's one way:

pd.DataFrame(df1.values/ df2.values.T, columns=df1.columns)

i ii iii
0 0.166667 0.285714 0.375
1 0.500000 0.571429 0.625

Pandas - divide DataFrame values by a Series in a MultiIndex DataFrame

just Use .values to set the values:

df.loc["bucket 1"]=df.loc["bucket 1"].div(s).values
print(df)

col1 col2 col3
bucket 1 q1 -0.149856 0.220604 -0.048464
q2 -0.791260 -0.199646 0.148115
q3 -0.712257 -0.264074 -0.266497
bucket 2 q1 -1.120164 -0.290546 0.577589
q2 -0.149522 -0.221203 -0.566872
q3 -1.002036 -2.233220 -1.206849

How I can divide each column of a dataframe with respect to values in another dataframe's column?

Try the following.

Example dfs:

from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()
df1 = spark.createDataFrame(
[('2021-03-01', 5),
('2021-03-02', 44),
('2021-03-03', 3),
('2021-03-04', 2),
('2021-03-05', 1)],
['dates', 'count']
)
df2 = spark.createDataFrame(
[('A', 40, 42, 30, 1, 8),
('B', 80, 3, 54, 2, 7),
('C', 10, 0, 52, 2, 8)],
['name', '2021-03-01', '2021-03-02', '2021-03-03', '2021-03-04', '2021-03-05']
)

Script:

# Unpivoting df2
cols_to_unpivot = [f"`{c}`, \'{c}\'" for c in df2.columns if c != 'name']
stack_string = ', '.join(cols_to_unpivot)
df2 = df2.select(
'name',
F.expr(f'stack({len(cols_to_unpivot)}, {stack_string}) as (val, dates)')
)

# Joining
df_joined = df2.join(df1, 'dates', 'full')

# Pivoting the result
df = df_joined.groupBy('name').pivot('dates').agg(F.first(F.col('val') / F.col('count')))

df.show()
# +----+----------+-------------------+------------------+----------+----------+
# |name|2021-03-01| 2021-03-02| 2021-03-03|2021-03-04|2021-03-05|
# +----+----------+-------------------+------------------+----------+----------+
# | B| 16.0|0.06818181818181818| 18.0| 1.0| 7.0|
# | C| 2.0| 0.0|17.333333333333332| 1.0| 8.0|
# | A| 8.0| 0.9545454545454546| 10.0| 0.5| 8.0|
# +----+----------+-------------------+------------------+----------+----------+

Panda Python - dividing a column by 100 (then rounding by 2.dp)

You can use div with round:

df = pd.DataFrame({'A':[61.75, 10.25], 'B':[0.62, 0.45]})
print (df)
A B
0 61.75 0.62
1 10.25 0.45

df['A'] = df['A'].div(100).round(2)
#same as
#df['A'] = (df['A'] / 100).round(2)
print (df)
A B
0 0.62 0.62
1 0.10 0.45


Related Topics



Leave a reply



Submit