简体   繁体   English

如何根据另一列中的连续两行添加 dataframe 列

[英]How to add a dataframe column based on two consecutive rows in another column

I have column (A) in this pandas dataframe我在这个 pandas dataframe 中有 (A) 列

A一种 B
1 1个 1 1个
1 1个 2 2个
2 2个 3 3个
5 5个 7 7

Column (B) is created using the following formula:列 (B) 使用以下公式创建:

col B (2)= col A(1)+ col A(2) B列(2)= A列(1)+ A列(2)

How do I create column (B) from column A?如何从 A 列创建 (B) 列?

Use shift() to get the rows one row down and add it back to column a .使用shift()将行向下一行并将其添加回列a

df['b'] = df['a'] + df['a'].shift(fill_value=0)

资源

You can do你可以做

df["B"] = df['A'] + df['A'].shift(1, fill_value= 0.)

暂无
暂无

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

相关问题 如何根据DataFrame中的另一列查找列的两行是否存在? - how to find if two rows of a column exist based on another column in a DataFrame? 如何在一个具有相同值(字符串)的数据框中找到两个连续的行,并在它们之间添加更多行? - how to find two consecutive rows in a dataframe with same value(string) for a column and add more rows between them? 如何根据另一列的两个连续值在 pandas 的新列中添加 label? - How can I add a label in a new column in pandas based on two consecutive values of another column? Python Pandas:如何在 Z6A8064B5DF4794555500553C47C55057DZ 的特定列中的两个非连续行中相减 - Python Pandas: How to subtract values in two non-consecutive rows in a specific column of a dataframe from one another Python 数据框 - 基于列删除连续行 - Python dataframe - drop consecutive rows based on a column 根据另一列中的两行计算数据框列 - calculate dataframe column based on two rows in another column 如何比较由另一列分组的两个连续行? - How to compare two consecutive rows grouped by another column? 根据来自另一个数据帧的行中的范围添加/填充 Pandas 列 - Add/fill pandas column based on range in rows from another dataframe 如何根据另一列的条件将新列添加到 dataframe - How to add a new column to dataframe based on conditions on another column 如何根据另一列的时间将列添加到pandas数据框 - How to add a column to pandas dataframe based on time from another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM