Row by row multiplication on pandas or numpy

I have one dataframe with classes and two components and a second one with elements and the same components. df1: df1 = pd.DataFrame({‘Class’:[‘banana’, ‘apple’], ‘comp1’:[1, 2], ‘comp2’:[-5, 4]}) df2: df2 = pd.DataFrame({‘Element’:[‘K’, ‘Mg’], ‘comp1’:[3, -4], ‘comp2’:[1, 3]}) I want to multiply them row by row in a way that would generate the following output: output… Read More Row by row multiplication on pandas or numpy

Number of values within a certain range in Python

I have an array A. I want to print total number of values in the range [1e-11,1e-7]. But I am getting an error. I present the expected output. import numpy as np A=np.array([ 4.21922009e+002, 4.02356746e+002, 3.96553289e-09, 3.91811967e-010, 3.88467908e-08, 3.86636300e-010]) B=1e-11<A<1e-07 print(B) The error is in <module> B=1e-11<A<1e-07 ValueError: The truth value of an array with… Read More Number of values within a certain range in Python

Numpy – Determine nearly equivalent floats before finding max value and index in 1D array

I have been stuck on this problem for hours. Let’s say we have a 1D array of floats: [0.12345678, 0.23456788, 0.23456789] I just need to find the max value of the array and it’s index. I know I can do that with np.max and np.argmax. res = np.max(array) index = np.argmax(array) In this case, the… Read More Numpy – Determine nearly equivalent floats before finding max value and index in 1D array

Weighted average of a dictionary – Pandas

I have the following column in a data-frame (it is an example): First row is: ‘{"100":10,"50":3,"-90":2}’. Second row is: ‘{"100":70,"50":3,"-90":2,"-40":3}’. I want to calculate a weighted average where the dictionary’s keys are the values and the dictionary’s values are the weights of the weighted average. The final value of the first row should be: 64.666,… Read More Weighted average of a dictionary – Pandas