根据动态输入获取列值

时间:2019-03-08 10:17:11

标签: python-3.x pandas

我有一个dataframe,其中我有1列,其中包含满足每行某些条件的列的名称。

这就像数据帧的列是索引,Col1, Col2, Col3Col_Name。其中Col_Name每行都有Col1 or Col2 or Col3

现在在新列中输入Col_New,我希望为每行输出,例如第5行Col_Name提到Col_1,然后是第5行Col_1的值。

对不起,我无法发布正在处理的代码,因此给出了这个假设示例。

有任何帮助,谢谢。

3 个答案:

答案 0 :(得分:1)

您可以使用的IIUC:

df['col_new'] = df.reset_index().apply(lambda x: df.at[x['index'], x['col_name']], axis=1)

示例:

cols = ['Col1', 'Col2', 'Col3']

df = pd.DataFrame(np.random.rand(10, 3), columns=cols)
df['Col_Name'] = np.random.choice(cols, 10)
print(df)

       Col1      Col2      Col3 Col_Name
0  0.833988  0.939254  0.256450     Col2
1  0.675909  0.609494  0.641944     Col3
2  0.877474  0.971299  0.218273     Col3
3  0.201189  0.265742  0.800580     Col2
4  0.397945  0.135153  0.941313     Col2
5  0.666252  0.697983  0.164768     Col2
6  0.863377  0.839421  0.601316     Col2
7  0.138975  0.731359  0.379258     Col3
8  0.412148  0.541033  0.197861     Col2
9  0.980040  0.506752  0.823274     Col3    

df['Col_New'] = df.reset_index().apply(lambda x: df.at[x['index'], x['Col_Name']], axis=1)

[出]

       Col1      Col2      Col3 Col_Name   Col_New
0  0.833988  0.939254  0.256450     Col2  0.939254
1  0.675909  0.609494  0.641944     Col3  0.641944
2  0.877474  0.971299  0.218273     Col3  0.218273
3  0.201189  0.265742  0.800580     Col2  0.265742
4  0.397945  0.135153  0.941313     Col2  0.135153
5  0.666252  0.697983  0.164768     Col2  0.697983
6  0.863377  0.839421  0.601316     Col2  0.839421
7  0.138975  0.731359  0.379258     Col3  0.379258
8  0.412148  0.541033  0.197861     Col2  0.541033
9  0.980040  0.506752  0.823274     Col3  0.823274

示例2(基于整数col引用)

cols = [1, 2, 3]
np.random.seed(0)
df = pd.DataFrame(np.random.rand(10, 3), columns=cols)
df[13] = np.random.choice(cols, 10)

print(df)

        1         2         3   13
0  0.548814  0.715189  0.602763   3
1  0.544883  0.423655  0.645894   3
2  0.437587  0.891773  0.963663   1
3  0.383442  0.791725  0.528895   3
4  0.568045  0.925597  0.071036   1
5  0.087129  0.020218  0.832620   1
6  0.778157  0.870012  0.978618   1
7  0.799159  0.461479  0.780529   2
8  0.118274  0.639921  0.143353   2
9  0.944669  0.521848  0.414662   3

代替使用:

df['Col_New'] = df.reset_index().apply(lambda x: df.at[int(x['index']), int(x[13])], axis=1)

          1         2         3  13   Col_New
0  0.548814  0.715189  0.602763   3  0.602763
1  0.544883  0.423655  0.645894   3  0.645894
2  0.437587  0.891773  0.963663   1  0.437587
3  0.383442  0.791725  0.528895   3  0.528895
4  0.568045  0.925597  0.071036   1  0.568045
5  0.087129  0.020218  0.832620   1  0.087129
6  0.778157  0.870012  0.978618   1  0.778157
7  0.799159  0.461479  0.780529   2  0.461479
8  0.118274  0.639921  0.143353   2  0.639921
9  0.944669  0.521848  0.414662   3  0.414662

答案 1 :(得分:0)

使用Chris A的示例DataFrame。

您可以这样做:

cols = ['Col1', 'Col2', 'Col3']
df = pd.DataFrame(np.random.rand(10, 3), columns=cols)
df['Col_Name'] = np.random.choice(cols, 10)

print(df)
df['Col_New'] = [df.loc[df.index[i],j]for i,j in enumerate(df.Col_Name)]

print(df)

答案 2 :(得分:0)

在pandas中,此功能DataFrame.lookup似乎也需要在列和外观列中使用相同类型的值,因此可以将它们都转换为字符串:

np.random.seed(123)
cols = [1, 2, 3]
df = pd.DataFrame(np.random.randint(10, size=(5, 3)), columns=cols).rename(columns=str)
df['Col_Name'] = np.random.choice(cols, 5)

df['Col_New'] = df.lookup(df.index, df['Col_Name'].astype(str))
print(df)

   1  2  3  Col_Name  Col_New
0  2  2  6         3        6
1  1  3  9         2        3
2  6  1  0         1        6
3  1  9  0         1        1
4  0  9  3         1        0
相关问题