python - 根据多个条件在 Pandas 数据框中创建一个新列

标签 python pandas dataframe conditional-statements

我有一个如下所示的数据框,我必须创建一个新列 year_val等于 col2016 的值通过col2019基于 Years列,以便 year_val 的值将是 col#### 的值什么时候Years等于col####的后缀

import pandas as pd

sampleDF = pd.DataFrame({'Years':[2016,2016,2017,2017,2018,2018,2019,2019],
                        'col2016':[1,2,3,4,5,6,7,8],
                        'col2017':[9,10,11,12,13,14,15,16],
                        'col2018':[17,18,19,20,21,22,23,24],
                        'col2019':[25,26,27,28,29,30,31,32]})

sampleDF['year_val'] = ?????

最佳答案

使用DataFrame.lookupYears 列中更改值,并在前面添加 col 并转换为字符串:

sampleDF['year_val'] = sampleDF.lookup(sampleDF.index, 'col' + sampleDF['Years'].astype(str))

print (sampleDF)
   Years  col2016  col2017  col2018  col2019  year_val
0   2016        1        9       17       25         1
1   2016        2       10       18       26         2
2   2017        3       11       19       27        11
3   2017        4       12       20       28        12
4   2018        5       13       21       29        21
5   2018        6       14       22       30        22
6   2019        7       15       23       31        31
7   2019        8       16       24       32        32

编辑:如果检查 lookup 函数的定义:

result = [df.get_value(row, col) for row, col in zip(row_labels, col_labels)]

您可以使用带有Series.attry-except 语句对其进行修改为了防止:

FutureWarning: get_value is deprecated and will be removed in a future release. Please use .at[] or .iat[] accessors instead oup.append(sampleDF.at[row, col] )

sampleDF = pd.DataFrame({'Years':[2015,2016,2017,2017,2018,2018,2019,2019],
                        'col2016':[1,2,3,4,5,6,7,8],
                        'col2017':[9,10,11,12,13,14,15,16],
                        'col2018':[17,18,19,20,21,22,23,24],
                        'col2019':[25,26,27,28,29,30,31,32]})

print (sampleDF)
   Years  col2016  col2017  col2018  col2019
0   2015        1        9       17       25
1   2016        2       10       18       26
2   2017        3       11       19       27
3   2017        4       12       20       28
4   2018        5       13       21       29
5   2018        6       14       22       30
6   2019        7       15       23       31
7   2019        8       16       24       32

out= []
for row, col in zip(sampleDF.index, 'col' + sampleDF['Years'].astype(str)):
    try:
        out.append(sampleDF.at[row, col] )
    except KeyError:
        out.append(np.nan)

sampleDF['year_val'] = out
print (sampleDF)
   Years  col2016  col2017  col2018  col2019  year_val
0   2015        1        9       17       25       NaN
1   2016        2       10       18       26       2.0
2   2017        3       11       19       27      11.0
3   2017        4       12       20       28      12.0
4   2018        5       13       21       29      21.0
5   2018        6       14       22       30      22.0
6   2019        7       15       23       31      31.0
7   2019        8       16       24       32      32.0

关于python - 根据多个条件在 Pandas 数据框中创建一个新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57711502/

相关文章:

python - 谷歌应用引擎和 polymer |配置

Python: "import"更喜欢什么——模块还是包?

python - 使用python中的正则表达式从字符串中提取不同格式的日期

python - 基于 bool 掩码选择行 - 为什么性能存在差异?

r - 数据框列命名

python - 在 Python 中使用列名构建 DataFrame

python - 将许多按钮绑定(bind)到一个函数,将每个按钮的名称作为参数传递

Python,在多个函数中共享 mysql 连接 - 传递连接或游标?

python - Pandas 将所有列的值连接到一个新的列列表中

python - 当时间戳未归类为索引时,如何按时间戳对数据帧进行切片?