我尝试用filter
和numpy.where
创造更多的通用的解决方案,为新的列名称使用extract
:
#if necessary sort columns
df = df.sort_index(axis=1)
#filter df by 1 and 2
df1 = df.filter(like='1')
df2 = df.filter(like='2')
print (df1)
C1 V1
0 1 2
1 5 6
2 9 10
print (df2)
C2 V2
0 3 4
1 7 8
2 11 12
#np.where need same shape of mask as df1 and df2
mask = pd.concat([df.Cond == 'X']*len(df1.columns), axis=1)
print (mask)
Cond Cond
0 True True
1 False False
2 True True
cols = df1.columns.str.extract('([A-Za-z])', expand=False)
print (cols)
Index(['C', 'V'], dtype='object')
print (np.where(mask, df1,df2))
Index(['C', 'V'], dtype='object')
[[ 1 2]
[ 7 8]
[ 9 10]]
print (pd.DataFrame(np.where(mask, df1, df2), index=df.index, columns=cols))
C V
0 1 2
1 7 8
2 9 10
可能的复制[Cre用熊猫的ELIF吃了一列](http://stackoverflow.com/questions/18194404/create-column-with-elif-in-pandas) – e4c5
'indexer = {'X':['C1','V1'] ,'Y':['C2','V2']};对于k,v in indexer.items()])中的pd.concat([pd.DataFrame(df.loc [df.Cond == k,v] .values,columns = ['C','V']))'是这样做的一种方式,但它不保存行的顺序。 – Abdou