Say I have this variables below ( id , a,b,c,d )
id a b c d
x 2 4 5 7
y 4 5 9
z 1 2
I want to create a new concatenate variable named ‘total’ from these strings , so I used this code below :
total = a + ' ' + b + ' ' + c + ' ' + d
Since I don’t want all these to be next to each other 2457 , I need one space blank ( ' ' ) between each variables 2 4 5 7, my result look something like this
id a b c d total
x 2 4 5 7 2 4 5 7
y 4 5 9 4 5 9
z 1 2 1 2
My problem is .. for example @ y between 5 & 9 , I only want one space instead two Or i want my result to look like this … can anyone show me how to accomplish this ? In SAS , I can easily use something to compress , not sure how can I do this in python ..
id a b c d total
x 2 4 5 7 2 4 5 7
y 4 5 9 4 5 9
z 1 2 1 2
Hopefully I’m not confusing anyone ~ , thanks π
One of the reasons to use
joininstead of manually concatenating things is that you can do more complex stuff more easily.First, if you turn your
a + ' ' + b + ' ' + c + ' ' + dinto ajoin:That doesn’t change anything yet.
But now, how do we say “all of the non-empty strings in
(a, b, c, d)“? Easy:So:
That’s it.
If the empty values aren’t empty strings (or
None) but, say,' ', you need to change the test. For example, maybe:If you don’t understand generator expressions, all of the following are roughly equivalent, and hopefully you’ll understand one:
In every case, the idea is the same: We have a sequence of 4 values, and we’re filtering it down to a sequence of 0 to 4 values by keeping only the ones that aren’t empty.
If we stuck with your explicit concatenation, this is still possible, it’s just much harder and uglier:
Which again gives you: