简体   繁体   English

从数据框中减去特定列的每一行并添加到列表 -python

[英]Subtract each row of specific column from dataframe and add to the list -python

I'm trying to subtract each ow of column A from DataFrame aa and add those to the list.我正在尝试从 DataFrame aa 中减去 A 列的每一行并将它们添加到列表中。

a = pd.DataFrame(np.random.randint(1,10,(5,4)),columns=['A','B','C','D']) list=[] a = pd.DataFrame(np.random.randint(1,10,(5,4)),columns=['A','B','C','D']) list=[]

I am so sorry for this such a basic question but I can't find the solution.对于这样一个基本问题,我感到很抱歉,但我找不到解决方案。

Edit: Just realized you only wanted to extract a column from a DataFrame as a list.编辑:刚刚意识到您只想从DataFrame提取一列作为列表。 In that case, like @arkhadiusz commented, you can just use:在这种情况下,就像@arkhadiusz 评论的那样,您可以使用:

a.A.to_list()

Note that this won't remove the column from your DataFrame .请注意,这不会从您的DataFrame删除该列。 If you'd like to completely remove it from your DataFrame , you can use:如果您想从DataFrame完全删除它,您可以使用:

a.pop('A').to_list()

Assuming you want to subtract a constant value from each element in a column of your DataFrame , you can try something like this:假设您想从DataFrame列中的每个元素中减去一个常量值,您可以尝试以下操作:

x = 0.5 # some constant value you want to subtract
[val1 - val2 for val1, val2 in zip(a.A.to_list(), [x] * a.shape[0])]

Or if you want to subtract different values for each element, you can try:或者,如果您想为每个元素减去不同的值,您可以尝试:

x = [1, 2, 3, 4, 5]
[val1 - val2 for val1, val2 in zip(a.A.to_list(), x)]

If I understand correctly, you want this:如果我理解正确,你想要这个:

To get a numpy array:要获得一个 numpy 数组:

lst = a[["B","C","D"]].subtract(a["A"], axis=0).values

To get a 2d list:要获得二维列表:

lst = a[["B","C","D"]].subtract(a["A"], axis=0).values.T.tolist()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从数据框中的第一行减去每一行 - Subtract each row from first row in dataframe 用 Python 中 Dataframe 上的前一列减去每一列 - Subtract each column by the preceding column on Dataframe in Python Python Pandas - 在特定行上添加列,将特定行从一个 dataframe 添加到另一个 - Python Pandas - add column on a specific row, add specific row from one dataframe to another 从特定列向数据帧添加一行 - Add a row to a dataFrame from a specific column 如何从 pandas dataframe 中的当前行中减去前一行以创建一个新列,以每个名称重新启动进程? - How to subtract previous row from current row in a pandas dataframe to create a new column restarting the process with each name? 是否有 pandas 方法通过特定列值为每一行添加 dataframe - Is there a pandas way to add a dataframe for each row by a specific column value 在dataframe列的每一行中查找单词并添加一个新列 - Python - Find for a word in a each row of dataframe column and add a new column - Python 计算 dataframe 中每一行和特定列在列表中的出现次数 - Count occurrences in a list for each row and specific column in a dataframe 将同一行从 pandas dataframe 多次添加到新行,每次更改特定列中的值 - Add the same row multiple times from a pandas dataframe to a new one, each time altering a value in a specific column Python:从每个新行从特定列表索引开始的列表创建 dataframe - Python: Create dataframe from a list where each new row starts at a specific list index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM