如何根据 Pandas DataFrame 中其他列的值创建新列 [英] How to create a new column based on values from other columns in a Pandas DataFrame

查看:131
本文介绍了如何根据 Pandas DataFrame 中其他列的值创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和 Pandas 的新手.因此,请不要严格判断.

I'm new to programming and Pandas. Therefore, please do not judge strictly.

在此表中,我需要添加一个新列,其中包含从其他列中获取的值.

To this table, I need to add a new column of values got from other columns.

inp = [{'Date':2003, 'b1':5,'b2':0,'b3':4,'b4':3},{'Date':2003, 'b1':2,'b2':2,'b3':1,'b4':8},{'Date':2004, 'b1':2,'b2':3,'b3':1,'b4':1},{'Date':2004, 'b1':1,'b2':8,'b3':2,'b4':1},{'Date':2005, 'b1':2,'b2':1,'b3':6,'b4':2},{'Date':2006, 'b1':1,'b2':7,'b3':2,'b4':9}]
df = pd.DataFrame(inp)
print (df)

   Date  b1  b2  b3  b4
0  2003   5   0   4   3
1  2003   2   2   1   8
2  2004   2   3   1   1
3  2004   1   8   2   1
4  2005   2   1   6   2
5  2006   1   7   2   9

也就是说,取决于日期.也就是说,如果列 "Date" == 2003 的值 - 我需要从列 b1 中获取值,如果列的值 "Date" =2004 然后我需要从 b2 列中获取值,"Date" = 2004 - b3 列等等.所以新列的值应该是:5,2,3,8,6,9.

Namely, depending on the date. That is if the value of column "Date" == 2003 - I need to get the value from column b1, if the value of column "Date" = 2004 then I need to get the value from column b2, "Date" = 2004 - column b3 and so on. So the values of new column should be: 5,2,3,8,6,9.

我有一本信函字典.喜欢:

I have a dictionary of correspondences smt. like:

Corr_dict = {2003:'b1',2004:'b2',2005:'b4',2006:'b7'...}

这只是一个例子.我有一个很大的数据集,所以我想了解一下机制.

This is just an example. I have a large dataset, so I want to understand the mechanics.

抱歉我的问题格式很差.如有任何帮助,我将不胜感激.

Sorry for the poor question formatting. I will be very grateful for any help.

预期输出

   Date  b1  b2  b3  b4  vals
0  2003   5   0   4   3   5.0
1  2003   2   2   1   8   2.0
2  2004   2   3   1   1   3.0
3  2004   1   8   2   1   8.0
4  2005   2   1   6   2   6.0
5  2006   1   7   2   9   9.0

推荐答案

我会使用 df.lookup:

df['Correspond'] = df.lookup(df.index, df['Date'].map(dd))

MCVE:

import pandas as pd

import numpy as np

inp = [{'Date':2003, 'b1':5,'b2':0,'b3':4,'b4':3},{'Date':2003, 'b1':2,'b2':2,'b3':1,'b4':8},{'Date':2004, 'b1':2,'b2':3,'b3':1,'b4':1},{'Date':2004, 'b1':1,'b2':8,'b3':2,'b4':1},{'Date':2005, 'b1':2,'b2':1,'b3':6,'b4':2},{'Date':2006, 'b1':1,'b2':7,'b3':2,'b4':9}]
df = pd.DataFrame(inp)

dd = {2003:'b1', 2004:'b2', 2005:'b3', 2006:'b4'}

df['Correspond'] = df.lookup(df.index, df['Date'].map(dd))
print(df)

输出:

   Date  b1  b2  b3  b4  Correspond
0  2003   5   0   4   3           5
1  2003   2   2   1   8           2
2  2004   2   3   1   1           3
3  2004   1   8   2   1           8
4  2005   2   1   6   2           6
5  2006   1   7   2   9           9

这篇关于如何根据 Pandas DataFrame 中其他列的值创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆