I have a df like this:
col1        col2
[1,3,4,5]   [3,3,6,2]
[1,4,5,5]   [3,8,4,3]
[1,3,4,8]   [8,3,7,2]
Trying to divide the elements in the lists in col1 and col2 together to get what's in the result column:
col1        col2        result
[1,3,4,5]   [3,3,6,2]   [.33,1,.66,2.5]
[1,4,5,5]   [3,8,4,3]   [.33,.5,1.25,1.66]
[1,3,4,8]   [8,3,7,2]   [.33,1,.57,4]
Tried a lot of different approaches - but always get an error.
Attempts:
#attempt1
df['col1'].div(df['col2'], axis=0)
#attempt2
from operator import truediv
for i in df.col1:
     a = np.array(df['col1'])
     for t in df.col2:
         b = np.array(df['col2'])
         x = a/b
         print(x)
#attempt3
for i in df.index:
    a = col1
    b = col2
    x = map(truediv, a, b)
#attempt4
a = col1
b = col2
result = [x/y for x, y in zip(a, b)]
#then apply to df
#attempt5
a = col1
b = col2
result = a/b
print(percent_matched)
#then #apply to df
>>>TypeError: unsupported operand type(s) for /: 'list' and 'list'
Any ideas?
Method 2: Pandas divide two columns using div() function It divides the columns elementwise. It accepts a scalar value, series, or dataframe as an argument for dividing with the axis. If the axis is 0 the division is done row-wise and if the axis is 1 then division is done column-wise.
Type "=A1/B1" in the formula bar.Replace "A1" and "B1" with the actual cell locations you want to divide. For example, if you want to divide column A by column B and the first values appear in row 1, you'd use A1 and B1.
.applymap to convert the columns to np.arrays.div to divide the columnsresult must be rounded, tack on .apply(lambda x: np.round(x, 3)), when calculating that column.
np.round()df['result'] = df.col1.div(df.col2).apply(lambda x: np.round(x, 3))import numpy as np
import pandas as pd
data = {'col1': [[1,3,4,5], [1,4,5,5], [1,3,4,8]], 'col2': [[3,3,6,2], [3,8,4,3], [8,3,7,2]]}
df = pd.DataFrame(data)
# convert columns to arrays
df = df.applymap(np.array)
# divide the columns
df['result'] = df.col1.div(df.col2)
                        You can use list comprehension with apply, this is conditional on both the lists being of same length
df['result'] = df.apply(lambda x: [np.round(x['col1'][i]/x['col2'][i], 2) for i in range(len(x['col1']))], axis = 1)
    col1            col2            result
0   [1, 3, 4, 5]    [3, 3, 6, 2]    [0.33, 1.0, 0.67, 2.5]
1   [1, 4, 5, 5]    [3, 8, 4, 3]    [0.33, 0.5, 1.25, 1.67]
2   [1, 3, 4, 8]    [8, 3, 7, 2]    [0.12, 1.0, 0.57, 4.0]
Edit: As @TrentonMcKinney suggested, this can be done without using LC. This solution capitalized on Numpy's vectorized operations,
df.apply(lambda x: np.round(np.array(x[0]) / np.array(x[1]), 3), axis=1)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With