0

I have a data frame that looks like this:

Index   Values                Digits 
1       [1.0,0.13,0.52...]       3
2       [1.0,0.13,0.32...]       3
3       [1.0,0.31,0.12...]       1
4       [1.0,0.30,0.20...]       2
5       [1.0,0.30,0.20...]       3

My output should be:

Index   Values                Digits 
1       [0.33,0.04,0.17...]       3
2       [0.33,0.04,0.11...]       3
3       [0.33,0.10,0.40...]       1
4       [0.33,0.10,0.07...]       2
5       [0.33,0.10,0.07...]       3

I believe that the Values column has a np.array within the cells? Is this technically an array.

I wish to parse out the Values column and divide all values within the array by 3.

My attempts have stopped at the parsing out of the values:

a = df(df['Values'].values.tolist())

2 Answers 2

1

IIUC, apply the list calculation

df.Values.apply(lambda x : [y/3 for y in x])
Out[1095]: 
0    [0.3333333333333333, 0.043333333333333335, 0.1...
1    [0.3333333333333333, 0.043333333333333335, 0.1...
Name: Values, dtype: object
#df.Values=df.Values.apply(lambda x : [y/3 for y in x])
Sign up to request clarification or add additional context in comments.

Comments

1

Created dataframe:

import pandas as pd
d = {'col1': [[1,10], [2,20]], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

created function:

def divide_by_3(lst):
    outpuut =[]
    for i in lst:
        outpuut.append(i/3.0)
    return outpuut

apply function :

df.col1.apply(divide_by_3`)

result:

0    [0.333333333333, 3.33333333333]
1    [0.666666666667, 6.66666666667]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.