I have a pandas DataFrame in which several columns contain list of object like the following:
Index A B C
0 [1,2,3,] [4,5,6,...20] [1,2,3,...,64]
1 [1,2,3,] [4,5,6,...20] [1,2,3,...,64]
..
..
n [1,2,3,] [4,5,6,...20] [1,2,3,...,64]
Of course numbers are just for reference. I would like to sum, let's say '10' to the first 32 values of the column 'C'. I tried to use apply fucntion with lambda in several ways without success. Could you help me? Thanks in advance!
It's not clear whether you want to:
Both can be accomplished using apply
with a lambda returning a list comprehension.
The former:
df.loc[0:32, 'C'] = df.loc[0:32, 'C'].apply(lambda lst: [x+10 for x in lst])
The latter:
df.C = df.C.apply(lambda lst: [x+10 if i < 32 else x for i, x in enumerate(lst)])
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.