简体   繁体   中英

Array in DataFrame Panda Python

I have this DataFrame. In Column ArraysDate contains many elements. I want to be able to number and run the for loop in the array of java. I have not found any solution, please tell me some ideas?. Ex with CustomerNumber = 4 , then ArraysDate have 3 elements ,and understood i1,i2,i3,i4 to use calculations in ArraysDate. Thanks you

 CustomerNumber ArraysDate 1 [ 1 13 ] 2 [ 3 ] 3 [ 0 ] 4 [ 2 60 30 40] 

If I understand correctly, you want to get an array of data from 'ArraysDate' based on column 'CustomerNumber'.

Basically, you can use loc

import pandas as pd
data = {'c': [1, 2, 3, 4], 'date': [[1,2],[3],[0],[2,60,30,40]]}
df = pd.DataFrame(data)

df.loc[df['c']==4, 'date']
df.loc[df['c']==4, 'date'] = df.loc[df['c']==4, 'date'].apply(lambda i: sum(i))

Result:

[2, 60, 30, 40]

   c    date
0  1  [1, 2]
1  2     [3]
2  3     [0]
3  4     132

You can use the lambda to sum all items in the array per row.

Step 1: Create a dataframe

import pandas as pd
import numpy as np
d = {'ID': [[1,2,3],[1,2,43]]}
df = pd.DataFrame(data=d)

Step 2: Sum the items in the array

df['ID2']=df['ID'].apply(lambda x: sum(x))
df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM