1

Let's say I have text data in a pandas data frame with multi-label.

   Text              Label
0  I love you        A, B
1  Thank you         C, D
2  You are welcome   A, B, C

I wanted to convert it to a text file, where each row is the sentence and separated by label __label__ sign, and the each label is separated just by a space

Therefore, the text file will look like this:

I love you __label__  A B
Thank you __label__ C D
You are welcome __label__ A B C
1
  • Can you share the code you’ve written so far in an attempt to meet the requirements you’ve described as a minimal reproducible example, along with a succinct explanation of where that attempt falls short of said requirements? We’re not going to write your code for you, but we can assist you if you can demonstrate a good-faith effort on your part before posting in accordance with How to Ask. Commented Jun 13, 2022 at 8:21

2 Answers 2

1
import pandas as pd

df = {
'Text': ['I love you', 'Thank you', 'You are welcome'],
'Label': ['A B', 'C D', 'A B C']
}

data = pd.DataFrame(df, columns=['Text', 'Label'])
print(data)

with open('read1me.txt', 'w') as f:
    for index, row in data.iterrows():
        text = row['Text']
        lbl = row['Label'].replace(',', '')
        f.write(f'{text}\t{"__label__"}\t{lbl}' + "\n")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But, the original label has commas.: A, B, C. I wanted to replace comma with just a white space
@JayaA ok ive edited my solution. we just need to use the replace function i.e row['Label'].replace(',', '')
1

You can do this with to_csv() and set the separator as ' __label__ ':

df.to_csv('filename.txt', sep=' __label__ ', header=False, index=False)

Comments

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.