How to parse out array from column inside a dataframe?

时间:2018-04-18 17:49:53

标签: python pandas

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 个答案:

答案 0 :(得分: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])

答案 1 :(得分: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]