How to Multiply a Single Column in a Data.Frame by a Number

Python: Pandas Dataframe how to multiply entire column with a scalar

Here's the answer after a bit of research:

df.loc[:,'quantity'] *= -1 #seems to prevent SettingWithCopyWarning 

How to multiply specific column from dataframe with one specific column in same dataframe?

Firstly, to get the columns which you have to multiply, you can use list comprehension and string function startswith. And then just loop over the columns and create new columns by muptiplying with Price

multiply_cols = [col for col in df.columns if col.startswith('S_')]
for col in multiply_cols:
df[col+'_New'] = df[col] * df['Price']

df

Sample Image

Multiplying all columns in dataframe by single column

Also try

df1 * t(C)
# F1 F2 F3
#1 2.0 2.0 2.0
#2 5.0 5.0 5.0
#3 16.0 16.0 16.0
#4 4.5 4.5 4.5

When we try to multiply data frames they must be of the same size.

df1 * C

error in Ops.data.frame(df1, C) :
‘*’ only defined for equally-sized data frames

t() turns C into a matrix, i.e. a vector with dimension attribute of length 4. This vector gets recycled when we multiply it with df1.

What would also work in the same way (and might be faster than transposing C):

df1 * C$C

or

df1 * unlist(C)

How to multiply specific values of a column with a specific value in R

We can create a logical condition

i1 <- dt$year == 2010
dt$wage_base[i1] <- dt$wage_base[i1] * 1.2

Or with data.table

library(data.table)
setDT(dt)[, wage_base := as.numeric(wage_base)
][year == 2010, wage_base := wage_base * 1.2]

Or with dplyr

library(dplyr)
dt %>%
mutate(wage_base = case_when(year == 2010 ~ wage_base * 1.2,
TRUE ~ as.numeric(wage_base)))

How can i multiply specific columns within one dataframe by a lookup to specific columns in another dataframe?

You should first use the merge function on your identifier column to have only one df with all the columns.

df1 = df1.merge(df2, how ='left', on=['identifier_column'])

Then your rows with same identifier will already be grouped together, and you only have to multiply the columns as you would do normally :

df1['column_3'] = df1['column_1'] * df1['column_2']

faster column-multiply in dataframe

To check which is faster you can check the time that it takes for each case:
In Ipython or Jupiter would be:

%%timeit
d['a'] * d['b']

For a dataframe like this one:

a = np.arange(0,10000)
b = np.ones(10000)

d = pd.DataFrame(np.vstack([a,b]).T, columns=["a","b"])

Get your multiplication:

1- in pandas

d['a'] * d['b']
81.2 µs ± 977 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

2 - in numpy. avoiding pandas overhead

d['a'].values * d['b'].values
9.21 µs ± 41.4 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

... If you are worried so much about speed, use just numpy. Take advantage of the nice feature of pandas to allow you to access the array with the feature values.

Multiply values across each column by weight in another data.frame in R

You can use sweep and match -

df[-1] <- sweep(df[-1],2, weights$V1[match(names(df[-1]),rownames(weights))],`*`)

df
# id a b d EE f
#1 this 0.7494769 -0.1743717 5.293633 NA 4.175490
#2 is 0.9081165 2.7259681 9.884781 NA -8.807325
#3 an -0.1399082 0.5559418 -4.374990 NA -3.922983
#4 example -0.5804764 0.7803247 -10.545803 NA 3.467420
#5 data.frame 0.7755359 -1.1257980 -10.848448 NA 3.161471
#6 for -2.3269836 1.2253498 12.256612 NA 5.404699
#7 stackoverflow 0.8065261 -1.3295363 -3.270386 NA -5.228465
#8 please -0.3535157 -0.4973664 4.212379 NA 2.099392
#9 help -0.3637245 1.6810690 4.156319 NA 2.157794
#10 me -1.1767322 0.4480855 -1.102092 NA -4.835156


Related Topics



Leave a reply



Submit