创建一个由其他列组合而成的列 [英] Create a column that's a combination of other columns
本文介绍了创建一个由其他列组合而成的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个3列的pandas数据框,我想创建一个新列,如下所示:
I have a pandas dataframe with 3 columns, and I want to create a new column as follows:
import pandas as pd
import numpy as np
np.random.seed(2019)
df = pd.DataFrame(np.random.randn(5,3), columns=['A','B','C'])
cols = df.abs().idxmax(axis = 1)
cols
0 C
1 A
2 A
3 B
4 B
dtype: object
我想创建一个列D
,该列在索引0处采用C
的值,在索引1处采用A
的类,等等.换句话说,D
根据对象cols
.我确定我可以遍历索引并选择列,但是有更好的方法吗?
I want to create a column D
that takes the value of C
at index 0, A
at index 1, etc.. In other words, D
takes values from the appropriate column as per the object cols
. I'm sure I can loop through the indices and pick the columns, but is there a better way to do this?
推荐答案
使用 查看全文