我有一個由串列作為元素組成的資料框。我想從每個串列中減去一個值并創建一個新列。我的代碼:
df = pd.DataFrame({'A':[[1,2],[4,5,6]]})
df
A
0 [1, 2]
1 [4, 5, 6]
# lets substract 1 from each list
val = 1
df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x[0],[val]*len(x[0]))],axis=1)
目前的解決方案:
IndexError: index 3 is out of bounds for axis 0 with size 2
預期的解決方案:
df
A A_new
0 [1, 2] [0, 1]
1 [4, 5, 6] [3, 4, 5]
uj5u.com熱心網友回復:
轉換成numpy
array
df['A_new'] = df.A.map(np.array)-1
Out[455]:
0 [0, 1]
1 [3, 4, 5]
Name: A, dtype: object
uj5u.com熱心網友回復:
df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x,[val]*len(x))])
您必須將串列傳遞給len
函式。這x
是串列本身。所以索引它,x[0]
只回傳一個給定背景關系錯誤的數字。這給出了輸出:
A A_new
0 [1, 2] [0, 1]
1 [4, 5, 6] [3, 4, 5]
uj5u.com熱心網友回復:
一個簡單的串列理解怎么樣:
df['new'] = [[i - 1 for i in l] for l in df['A']]
A new
0 [1, 2] [0, 1]
1 [4, 5, 6] [3, 4, 5]
uj5u.com熱心網友回復:
您可以將串列轉換為np.array
然后減去val
:
import numpy as np
df['A_new'] = df['A'].apply(lambda x: np.array(x) - val)
輸出:
A A_new
0 [1, 2] [0, 1]
1 [4, 5, 6] [3, 4, 5]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/469389.html