1

My data set comes from jira and the Labels data is separated into multiple columns, one for each label in the row. This number of rows can vary from 1 to more than 5 depending on how many label tags were used on a given entry.

The csv could look like:

Issue Type  Issue key   Labels    Labels    Labels    Labels    Labels
Story       123         #label1,  #label2,  #label6,  #label7,  #label9,
Story       124                 
Story       125         #label3,  #label1,          
Bug         126                 
Story       127         #label5,        

The number of columns can vary getting a new Labels column for every tag in the row. There seems to be no way to correct the export to enclose the values as a single string.

What I'm needing to do is concatenate these into a single column "Tags", and I don't care about cleaning up the trailing comma.

I've tried

df['Tags'] = [col for col in df.columns if 'Label' in col]

But this throws an error "Length of values does not match length of index"

Is there a simple way to accomplish this when reading the CSV into the dataframe?

1
  • kindly share ur expected output. dataframe form Commented May 5, 2020 at 22:36

2 Answers 2

1

You can use agg function:

colums = [col for col in df.columns if col.startswith('value')]
df[columns].agg(lambda x : '-'.join(x.astype(str)), axis=1)
Sign up to request clarification or add additional context in comments.

3 Comments

Nice, I have never aggregated on axis = 1 :)
This throws the error: TypeError: ('sequence item 1: expected str instance, float found', 'occurred at index 0')
@J.P. I edited the answer for not object type columns
0

I think what you are really trying to do is:

tags = [col for col in df.columns if 'Label' in col]
nontags = [col for col in df.columns if 'Label' non in col]
tagdf = df[tags]
tagcol = tagdf.apply(" ".join, axis=1)
newdf = pd.concat([df[nontags], tagcol], axis = 1)

2 Comments

NameError: name 'join' is not defined - and .join is invalid syntax..
@J.P. It was a typo, which you should be able to fix in about 20 seconds.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.