简体   繁体   中英

join 2 columns with same name by pandas data frame

I am a beginner in pandas. I have an input like

num. first second  x    x.1  x.2 x.3  last
1     ah     ro    hg   rl   ew  wk   o2
2     as     ht    hf   cd   ek  qi   4j
3     uy     rf    kh   we   ls  qj   ke

And the output would be

num. first second    x          last
1     ah     ro    hg,rl,ew,wk   o2
2     as     ht    hf,cd,ek,qi   4j
3     uy     rf    kh,we,ls,qj   ke

Here's a solution:

cols = df.filter(regex='^x').columns
df['x'] = df[cols].agg(','.join, axis=1)
df = df.drop(cols, axis=1)

Output:

   num. first second last         x
0     1    ah     ro   o2  rl,ew,wk
1     2    as     ht   4j  cd,ek,qi
2     3    uy     rf   ke  we,ls,qj

Try this:

col_x = [*filter(lambda x: x.startswith('x.'), df.columns)]
df['x'] = df[col_x].apply(lambda row: ','.join(row), axis=1)
df = df.drop(col_x, axis=1)

You could try this -

df['x'] = df['x.1'] + ',' + df['x.2'] + ',' + df['x.3']
df = df.drop(['x.1', 'x.2', 'x.3'], axis=1)
df['x'] = df['x.1'] + "," +  df['x.2'] + "," + df['x.3']
df.drop(['x.1', 'x.2', 'x.3'], axis = 1, inplace = True)

Try:

df["x"]=df["x.1"].cat([df["x.2"], df["x.3"]], sep=",")
# then to drop all x.n:
df=df.drop(["x.1", "x.2", "x.3"], axis=1)

It's quite easy that:

l1 = ['hg', 'rl', 'ew', 'wk']
','.join(l1) # hg,rl,ew,wk

Then what we need is just do this to column x~x.3 of each line, so we use df.apply(..., axis=1)

df1.apply(lambda x:','.join(x[2:6]), axis=1)

Then we get a new pd.Series which is what you expected, just assign it to the the former DataFrame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM