R multiply columns by values in second dataframe
Tag : r , By : jumpingmattflash
Date : March 29 2020, 07:55 AM
wish of those help I have two dataframes: df1 , Your data: df1 <- read.table(
text =
"plot grass moss rock other stuff
a 4 0 0 text 3
b 2 2 0 text 4
c 10 0 0 text 0
d 3 1 0 text 9",
header = TRUE
)
df2 <- read.table(
text =
"Cover value
grass 5
moss 2
rock 3",
header = TRUE,
stringsAsFactors = FALSE
)
df3 <- df1
for(i in seq_len(nrow(df2)))
{
df3[, df2$Cover[i]] <- df2$value[i] * df1[, df2$Cover[i]]
}
|
How to multiply all hourly values in one pandas dataframe with yearly values in another dataframe?
Date : March 29 2020, 07:55 AM
wish helps you You can try append to index of df1 df1.index.year, then change index of df2 to years and then use mul: print df1
Value1 Value2
Date/Time
2010-01-03 00:00:00 60 10
2010-01-03 01:00:00 50 20
2010-01-03 02:00:00 52 30
2010-01-03 03:00:00 49 40
2013-12-31 23:00:00 77 50
print df2
Value1 Value2
Date/Time
2010-12-31 1.5 0.9
2011-12-31 1.6 1.1
2012-12-31 1.7 2.3
2013-12-31 1.3 0.6
df1 = df1.set_index(df1.index.year, append=True)
df2.index = df2.index.year
print df1
Value1 Value2
Date/Time
2010-01-03 00:00:00 2010 60 10
2010-01-03 01:00:00 2010 50 20
2010-01-03 02:00:00 2010 52 30
2010-01-03 03:00:00 2010 49 40
2013-12-31 23:00:00 2013 77 50
print df2
Value1 Value2
2010 1.5 0.9
2011 1.6 1.1
2012 1.7 2.3
2013 1.3 0.6
print df1.mul(df2, level=1).reset_index(drop=True, level=1)
Value1 Value2
Date/Time
2010-01-03 00:00:00 90.0 9
2010-01-03 01:00:00 75.0 18
2010-01-03 02:00:00 78.0 27
2010-01-03 03:00:00 73.5 36
2013-12-31 23:00:00 100.1 30
|
Multiply dataframe with values from other dataframe
Tag : python , By : Ronnie Carlin
Date : March 29 2020, 07:55 AM
wish of those help I have two dataframes , Use set_index and reindex to align df2 with df1 and then mul In [1150]: df1.mul(df2.set_index(0).reindex(df1.index)[1], axis=0)
Out[1150]:
d e
a 10 20
b 60 80
c 150 180
a 70 80
|
Iterate over every row in pandas dataframe and multiply all row values by one of the row values in same dataframe
Date : March 29 2020, 07:55 AM
hope this fix your issue You can using str.contains , then assign the mul result back with .loc , also here since you need assign it back ,with filter will failed s=df.columns.str.contains('Count')
df.loc[:,s]=df.loc[:,s].mul(df['Normalization Value'],0)
df
Out[238]:
Sample Timepoint ... CountC Normalization Value
0 1 1 ... 5.0 0.1
1 2 1 ... 20.0 0.2
2 2 2 ... 20.0 0.5
[3 rows x 8 columns]
|
Conditionally multiply values in a dataframe by another dataframe if above a certain threshold in R
Date : March 29 2020, 07:55 AM
wish of those help We can get the indices where Pdist is greater than threshold and then multiply. inds <- dataframe_1$Pdist > threshold
dataframe_1$Pdist[inds] <- dataframe_1$Pdist[inds] * dataframe_2$Pdist[inds]
|