Python Dataframe subtract a value from each list of a row
I have a data frame consisting of lists as elements. I want to subtract a value from each list and create a new column.My code:df = pd.DataFrame({'A':[[1,2],[4,5,6]]})df A0 [1, 2]1 [4, 5, 6]# lets...
View ArticleAnswer by user2736738 for Python Dataframe subtract a value from each list of...
df['A_new'] = df['A'].apply(lambda x:[a-b for a,b in zip(x,[val]*len(x))])You have to pass the list to the len function. Here x is the list itself. So indexing it, x[0] just returns a number which is...
View ArticleAnswer by BENY for Python Dataframe subtract a value from each list of a row
Convert to numpyarraydf['A_new'] = df.A.map(np.array)-1Out[455]: 0 [0, 1]1 [3, 4, 5]Name: A, dtype: object
View ArticleAnswer by Shubham Sharma for Python Dataframe subtract a value from each list...
How about a simple list comprehension:df['new'] = [[i - 1 for i in l] for l in df['A']] A new0 [1, 2] [0, 1]1 [4, 5, 6] [3, 4, 5]
View ArticleAnswer by Abhyuday Vaish for Python Dataframe subtract a value from each list...
You can convert the list to np.array and then subtract the val:import numpy as npdf['A_new'] = df['A'].apply(lambda x: np.array(x) - val)Output: A A_new0 [1, 2] [0, 1]1 [4, 5, 6] [3, 4, 5]
View Article