简体   繁体   English

Pandas 从同一数据框中查找条件,然后添加到右侧作为新列

[英]Pandas lookup from same dataframe for criteria then add to right as new column

My goal is to create an excel-vlookup-equivalent in python which takes the value of the past month and adds it to a new column to the current month, ie id, month, value_current_month, value_past_month:我的目标是在 python 中创建一个 excel-vlookup-equivalent,它采用上个月的值并将其添加到当前月份的新列中,即 id、month、value_current_month、value_past_month:

From This:由此:

id  month  value
01     09    123
02     09    234
03     09    345
01     08    543
02     08    432
03     08    321
01     07    678
02     07    789
03     07    890
..     ..    ...

To this:对此:

id  month  value  new
01     09    123  543
02     09    234  432
03     09    345  321
01     08    543  678
02     08    432  789
03     08    321  890
01     07    678  ...
02     07    789  ...
03     07    890  ...
..     ..    ...  ...

I have imported pandas and numpy and created a dataframe called "df".我已经导入了 pandas 和 numpy 并创建了一个名为“df”的数据框。 As I am unfamiliar with the syntax of python, any help would be greatly appreciated.由于我不熟悉 python 的语法,任何帮助将不胜感激。

Thank you!谢谢!

  1. The proper way to do this is to create a Date column (since you will likely have multiple years, you cannot just join on month)正确的方法是创建一个Date列(因为您可能有多个年份,您不能只在月份加入)
  2. Then, merge the dataframe back on itself but shifted one month with + pd.DateOffset(months=1) .然后,将数据帧合并回自身,但使用+ pd.DateOffset(months=1)移动一个月。 and join on Date and id :并加入Dateid

#sample dataframe setup
import pandas as pd
df = pd.DataFrame({'id': {0: '01',1: '02',2: '03',3: '01',4: '02',5: '03',6: '01',7: '02',8: '03'},
'month': {0: '09',1: '09',2: '09',3: '08',4: '08',5: '08', 6: '07',7: '07',8: '07'},
'value': {0: 123,1: 234,2: 345,3: 543,4: 432,5: 321, 6: 678,7: 789,8: 890}})
df

#solution 1
df['Year'] = '2020'
df['Date'] = pd.to_datetime(df['Year'] + '-' + df['month'])
df = (pd.merge(df, df[['Date', 'value', 'id']].rename({'value' : 'new_value'}, axis=1)
                                              .assign(Date=df['Date'] + pd.DateOffset(months=1)),
                      how='left', on=['Date' , 'id']).drop('Date', axis=1))
df
Out[1]: 
   id month  value  Year  new_value
0   1    09    123  2020      543.0
1   2    09    234  2020      432.0
2   3    09    345  2020      321.0
3   1    08    543  2020      678.0
4   2    08    432  2020      789.0
5   3    08    321  2020      890.0
6   1    07    678  2020        NaN
7   2    07    789  2020        NaN
8   3    07    890  2020        NaN

Use .shift(-3) .使用.shift(-3) if the problem is simple and you have three ID values per month.如果问题很简单并且您每个月有三个 ID 值。 You can change -3 to -12 for example if you have 12 id values in your actual dataframe per month.例如,如果您每个月的实际数据框中有 12 个id值,您可以将-3更改为-12 This also assumes you have sorted your dataframe:这也假设您已经对数据框进行了排序:

#solution 2
df['new'] = df['value'].shift(-3)
df

Out[2]: 
   id  month  value    new
0   1      9    123  543.0
1   2      9    234  432.0
2   3      9    345  321.0
3   1      8    543  678.0
4   2      8    432  789.0
5   3      8    321  890.0
6   1      7    678    NaN
7   2      7    789    NaN
8   3      7    890    NaN

暂无
暂无

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

相关问题 pandas:根据相同 dataframe 的日期时间索引查找添加新列 - pandas: add new column based on datetime index lookup of same dataframe Pandas 使用来自另一个 DataFrame 的多个列的查找添加新列 - Pandas Add New Column using Lookup using Multiple Columns from another DataFrame 基于标签在同一数据框中的查找值,然后添加到新列(Vlookup) - Lookup value in the same dataframe based on label and add to a new column (Vlookup) 将新列添加到Pandas DataFrame,并用同一df的另一列填充第一个单词 - Add new column to Pandas DataFrame and fill with first word from another column from same df pandas dataframe 添加基于查找值的列 - pandas dataframe add column based on lookup values 通过使用另一列中的值来查找字典中的值,将新列添加到Pandas DataFrame - Add a new column to a Pandas DataFrame by using values in another column to lookup values in a dictionary 如何向 Pandas df 添加一个新列,该列从另一个数据帧返回同一组中较大的最小值 - How to add a new column to a pandas df that returns the smallest value that is greater in the same group from another 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 向 pandas dataframe 添加一个新列,其中包含来自另一列的转换值? - Add a new column to pandas dataframe with coverted values from another column? 寻找查找代码以向从同一数据帧查询的 Pandas 数据帧添加列 - Looking for lookup code to add columns to Pandas dataframe queried from same dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM