简体   繁体   English

在从其他列获取输入的 Pandas 数据框中填充新列

[英]populate new column in a pandas dataframe which takes input from other columns

i have a function which should take x , y , z as input and returns r as output.我有一个函数,它应该将 x , y , z 作为输入并返回 r 作为输出。 For example : my_func( x , y, z) takes x = 10 , y = 'apple' and z = 2 and returns value in column r.例如: my_func( x , y, z) 接受 x = 10 , y = 'apple' 和 z = 2 并返回 r 列中的值。 Similarly, function takes x = 20, y = 'orange' and z =4 and populates values in column r.类似地,函数采用 x = 20、y = 'orange' 和 z =4 并填充 r 列中的值。 Any suggestions what would be the efficient code for this ?任何建议什么是有效的代码?

Before :前 :

   a  x       y       z      
   5  10   'apple'    2
   2  20   'orange'   4
   0  4    'apple'    2
   5  5    'pear'     6

After:后:

   a  x       y       z      r
   5  10   'apple'    2      x
   2  20   'orange'   4      x
   10  4   'apple'    2      x
   5  5    'pear'     6      x

Depends on how complex your function is.取决于您的功能有多复杂。 In general you can use pandas.DataFrame.apply :一般来说,您可以使用pandas.DataFrame.apply

>>> def my_func(x):
...     return '{0} - {1} - {2}'.format(x['y'],x['a'],x['x'])
... 
>>> df['r'] = df.apply(my_func, axis=1)
>>> df
   a   x         y  z                  r
0  5  10   'apple'  2   'apple' - 5 - 10
1  2  20  'orange'  4  'orange' - 2 - 20
2  0   4   'apple'  2    'apple' - 0 - 4
3  5   5    'pear'  6     'pear' - 5 - 5

axis=1 is to make your function work 'for each row' instead of 'for each column`: axis=1是让你的函数“为每一行”而不是“为每一列”工作:

Objects passed to functions are Series objects having index either the DataFrame's index (axis=0) or the columns (axis=1)传递给函数的对象是具有索引 DataFrame 的索引 (axis=0) 或列 (axis=1) 的 Series 对象

But if it's really simple function, like the one above, you probably can even do it without function, with vectorized operations.但是如果它真的是简单的函数,就像上面的那个,你甚至可以不用函数,用向量化操作来做。

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

相关问题 如何在新列中填充值 - How to populate values inside a new column based values from other columns in a dataframe in Pandas 如何从数据框中的其他列创建新的Pandas数据框列 - How to create a new Pandas dataframe column from other columns in the dataframe python pandas dataframe从其他列的单元格创建新列 - python pandas dataframe create new column from other columns' cells Pandas dataframe:根据其他列的数据创建新列 - Pandas dataframe: Creating a new column based on data from other columns 从熊猫数据框中的其他列创建新列 - Creating new column from other columns in pandas dataframe 根据行值使用其他列的名称填充新的 Pandas 数据框列 - Populate a new pandas dataframe column with names of other columns based on their row value Pandas 根据另一个数据框中的匹配列填充新的数据框列 - Pandas populate new dataframe column based on matching columns in another dataframe 使用两列连接,在四个其他数据帧中填充一个pandas数据帧中的列 - using two column join, populate columns in one pandas dataframe from four other dataframes 根据条件和前一行值从其他列填充 Pandas Dataframe 列 - Populate Pandas Dataframe column from other columns based on a condition and previous row value 从 pandas dataframe 中的列创建新列 - Creating new column from columns in pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM