简体   繁体   English

在 dataframe 中创建一个新列,其中包含列表中该行的其他列的值

[英]Create a new column in the dataframe that has values of other columns for that row in a list

I want to change我想改变
在此处输入图像描述

This datafrme to the这个数据框到

在此处输入图像描述

how should I use apply function to achive this?我应该如何使用 apply function 来实现这一目标?

Try this:尝试这个:

df['bbox'] = df.apply(lambda x: [y for y in x], axis=1)

so for a df that looks like:所以对于一个看起来像这样的df:

In [15]: df
Out[15]:
   a  b  c
0  1  3  1
1  2  4  1
2  3  5  1
3  4  6  1

you'll get:你会得到:

In [16]: df['bbox'] = df.apply(lambda x: [y for y in x], axis=1)

In [17]: df
Out[17]:
   a  b  c       bbox
0  1  3  1  [1, 3, 1]
1  2  4  1  [2, 4, 1]
2  3  5  1  [3, 5, 1]
3  4  6  1  [4, 6, 1]

Hope this helps!希望这可以帮助!

As per your example to achieve required result, you need to convert each row in list.根据您的示例以达到所需的结果,您需要转换列表中的每一行。 Add that list to new DataFrame.将该列表添加到新的 DataFrame。 Once you add new list to DataFrame apply whatever calculation(your output DataFrame values are different from input DataFrame hence expecting you have done some calculation on each cell or row) you want to apply on the same. Once you add new list to DataFrame apply whatever calculation(your output DataFrame values are different from input DataFrame hence expecting you have done some calculation on each cell or row) you want to apply on the same.

import pandas as pd
data = {'x':[121,216,49],'y':[204,288,449],'w':[108,127,184]}
df = pd.DataFrame(data,columns=['x','y','w'])
new_data = [[row.to_list()] for i, row in df.iterrows()]
new_df = pd.DataFrame(new_data, columns='bbox')
print(new_df)

            bbox
0 [121, 216, 49]
1 [204, 288,449]
2 [108, 127, 184]

暂无
暂无

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

相关问题 使用具有字典列表的数据框的列为数据框创建其他列 - Use the column of a dataframe that has a list of dictionaries to create other columns for the dataframe 根据其他列行中的过滤值在 pandas dataframe 中创建一个新列 - Create a new Column in pandas dataframe based on the filetered values in the row of other columns 如何根据其他列的值在数据框中创建新列? - How to create a new column in a dataframe based off values of other columns? 根据其他列中的“NaN”值在 Pandas Dataframe 中创建一个新列 - Create a new column in Pandas Dataframe based on the 'NaN' values in other columns 从其他 2 列的值创建新的 dataframe 列 - Create new dataframe column from the values of 2 other columns Pandas dataframe 创建新列,指示其他列中的重叠值 - Pandas dataframe create new column indicating overlapping values in other columns Pandas:根据我的 dataframe 中的其他值列表创建一个新列 - Pandas: Create a new column based on a list of other values in my dataframe 根据其他 pandas 列中列表中的值数创建新列? - Create new columns based on number of values in list in other pandas column? 如何在 pandas dataframe 中通过在两行之间划分特定列中的值并保持其他列不变来创建新行? - How to create a new row in pandas dataframe by dividing values in a specific column between two rows and keeping other columns intact? 如何为其他两列的每个组合创建一个带有新列的新数据框行? - How to create a new dataframe row with a new column for every combination of other two columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM