通过从先前索引列计算,在pandas数据框中创建新列

时间:2021-07-14 22:57:34

Hi I am new and learning pandas for data analysis. I have 2 columns data

嗨,我是新手,学习大熊猫进行数据分析。我有2列数据

A   B
1   2   
2   3
3   4
4   5

I want to create a third column C which result would be calculated by column B , by subtracting upper value with current one and dividing by current.

我想创建第三列C,其结果将由列B计算,通过用当前值减去上限值并除以当前值。

A   B   C
1   2   
2   3   0.33
3   4   0.25
4   5   0.2

for example first row value for C column is empty because there is no value above 2 .

例如,C列的第一行值为空,因为没有大于2的值。

0.33 = > 3 - 2 / 3 , 
0.25 = > 4 - 3 / 4 ,
0.2 = > 5 - 4 / 5 and so on

I am stuck while getting the upper value of current column. Need help how to achieve that.

获取当前列的上限值时卡住了。需要帮助如何实现这一目标。

3 个解决方案

#1


3  

Use shift to shift the column and then the remaining operations are the regular ones (sub and div):

使用shift移动列,然后剩余的操作是常规操作(sub和div):

df['B'].sub(df['B'].shift()).div(df['B'])
Out: 
0         NaN
1    0.333333
2    0.250000
3    0.200000
Name: B, dtype: float64

This can also be done without chaining the methods, if you prefer.

如果您愿意,也可以在不链接方法的情况下完成此操作。

(df['B'] - df['B'].shift()) / df['B']
Out[48]: 
0         NaN
1    0.333333
2    0.250000
3    0.200000
Name: B, dtype: float64

#2


3  

Edit for handling NaN and decimals.

df['C'] = (1 - df.B.shift() / df.B).map(lambda x: '{0:.2f}'.format(round(x,2))).replace('nan','')

Output:

输出:

   A  B     C
0  1  2      
1  2  3  0.33
2  3  4  0.25
3  4  5  0.20

Let's simplify and use the following with shift to get the previous value:

让我们简化并使用以下的shift来获取前一个值:

df['C'] = 1 - df.B.shift() / df.B

Output:

输出:

   A  B         C
0  1  2       NaN
1  2  3  0.333333
2  3  4  0.250000
3  4  5  0.200000

#3


1  

Or you can simply using diff

或者你可以简单地使用diff

df2.B.diff()/df2.B
Out[545]: 
0         NaN
1    0.333333
2    0.250000
3    0.200000
Name: B, dtype: float64

#1


3  

Use shift to shift the column and then the remaining operations are the regular ones (sub and div):

使用shift移动列,然后剩余的操作是常规操作(sub和div):

df['B'].sub(df['B'].shift()).div(df['B'])
Out: 
0         NaN
1    0.333333
2    0.250000
3    0.200000
Name: B, dtype: float64

This can also be done without chaining the methods, if you prefer.

如果您愿意,也可以在不链接方法的情况下完成此操作。

(df['B'] - df['B'].shift()) / df['B']
Out[48]: 
0         NaN
1    0.333333
2    0.250000
3    0.200000
Name: B, dtype: float64

#2


3  

Edit for handling NaN and decimals.

df['C'] = (1 - df.B.shift() / df.B).map(lambda x: '{0:.2f}'.format(round(x,2))).replace('nan','')

Output:

输出:

   A  B     C
0  1  2      
1  2  3  0.33
2  3  4  0.25
3  4  5  0.20

Let's simplify and use the following with shift to get the previous value:

让我们简化并使用以下的shift来获取前一个值:

df['C'] = 1 - df.B.shift() / df.B

Output:

输出:

   A  B         C
0  1  2       NaN
1  2  3  0.333333
2  3  4  0.250000
3  4  5  0.200000

#3


1  

Or you can simply using diff

或者你可以简单地使用diff

df2.B.diff()/df2.B
Out[545]: 
0         NaN
1    0.333333
2    0.250000
3    0.200000
Name: B, dtype: float64