2017-07-12 75 views
1

我正在寻找一种方法来合并重复列,假设空白为NaN合并重复列熊猫

Column1[1] Column1[2] Column1[3] Column1[4] Column1[4] Column1[5] Column1[6] Column1[7] 
    a 123       
    b   432      
    c       53     
    d         221    
    e             2   
    f                3  
    g                   3243  
    h                      12 

输出应该是这样的

Row Column1[ALL] 
    a 123 
    b 432 
    c 53 
    d 221 
    e 2 
    f 3 
    g 3243 
    h 12 

回答

1

如果DF是你的数据框:

df.max(axis=1) 
1

下面是一些简便的方法来实现它,推广到所有数据类型

考虑数据框df

v = np.empty((8, 8), dtype=object) 
v.fill(None) 

i = np.arange(8) 

v[i, i] = [123, 432, 53, 221, 2, 3, 'hello', 12] 

df = pd.DataFrame(v, list('abcdefgh'), ['Column1[%s]' % i for i in range(1, 9)]) 

df 

    Column1[1] Column1[2] Column1[3] Column1[4] Column1[5] Column1[6] Column1[7] Column1[8] 
a  123  None  None  None  None  None  None  None 
b  None  432  None  None  None  None  None  None 
c  None  None   53  None  None  None  None  None 
d  None  None  None  221  None  None  None  None 
e  None  None  None  None   2  None  None  None 
f  None  None  None  None  None   3  None  None 
g  None  None  None  None  None  None  hello  None 
h  None  None  None  None  None  None  None   12 

选项1
stack默认下降零点。如果每行只有一个值,这将按需要工作。

df.stack() 

a Column1[1]  123 
b Column1[2]  432 
c Column1[3]  53 
d Column1[4]  221 
e Column1[5]  2 
f Column1[6]  3 
g Column1[7] hello 
h Column1[8]  12 
dtype: object 

或者

df.stack().reset_index(1, drop=True) 

a  123 
b  432 
c  53 
d  221 
e  2 
f  3 
g hello 
h  12 
dtype: object 

选项2
applydropna

df.apply(lambda x: x.dropna()[0], 1) 

a  123 
b  432 
c  53 
d  221 
e  2 
f  3 
g hello 
h  12 
dtype: object 

选项3
np.where组合和pd.DataFrame.lookup

i, j = np.where(df.notnull()) 
idx = df.index[i] 
col = df.columns[j] 

pd.Series(df.lookup(idx, col), idx) 

a  123 
b  432 
c  53 
d  221 
e  2 
f  3 
g hello 
h  12 
dtype: object