Python Dataframe 从一行的每个列表中减去一个值
Python Dataframe subtract a value from each list of a row
我有一个由列表作为元素组成的数据框。我想从每个列表中减去一个值并创建一个新列。
我的代码:
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]
df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x,[val]*len(x))])
您必须将列表传递给 len
函数。这里 x
是列表本身。所以索引它,x[0]
只是 returns 一个数字,在上下文中是错误的。这给出了输出:
A A_new
0 [1, 2] [0, 1]
1 [4, 5, 6] [3, 4, 5]
转换为 numpy
array
df['A_new'] = df.A.map(np.array)-1
Out[455]:
0 [0, 1]
1 [3, 4, 5]
Name: A, dtype: object
简单的列表理解如何:
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]
您可以将列表转换为 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]
我有一个由列表作为元素组成的数据框。我想从每个列表中减去一个值并创建一个新列。 我的代码:
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]
df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x,[val]*len(x))])
您必须将列表传递给 len
函数。这里 x
是列表本身。所以索引它,x[0]
只是 returns 一个数字,在上下文中是错误的。这给出了输出:
A A_new
0 [1, 2] [0, 1]
1 [4, 5, 6] [3, 4, 5]
转换为 numpy
array
df['A_new'] = df.A.map(np.array)-1
Out[455]:
0 [0, 1]
1 [3, 4, 5]
Name: A, dtype: object
简单的列表理解如何:
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]
您可以将列表转换为 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]