Reputation: 9
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
Upvotes: 0
Views: 62
Reputation: 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.
Upvotes: 0
Reputation: 150735
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
Upvotes: 2
Reputation: 4618
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)
Upvotes: 0
Reputation: 13387
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)
Upvotes: 0
Reputation: 1504
df['x'] = df['x.1'] + "," + df['x.2'] + "," + df['x.3']
df.drop(['x.1', 'x.2', 'x.3'], axis = 1, inplace = True)
Upvotes: 0
Reputation: 1267
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)
Upvotes: 0