I have a dataframe as follows:
df
Name Sequence
abc ghijklmkhf
bhf uyhbnfkkkkkk
dmf hjjfkkd
I want to append the second column data to the below of the first column data in specific format as follows:
Name Sequence Merged
abc ghijklmkhf >abc
ghijklmkhf
bhf uyhbnfkkkkkk >bhf
uyhbnfkkkkkk
dmf hjjfkkd >dmf
hjjfkkd
I tried df['Name'] = '>' df['Name'].astype(str)
to get the name in the format with >
symbol. How do I append the second column data below the value of first column data?
CodePudding user response:
You can use vectorial concatenation:
df['Merged'] = '>' df['Name'] '\n' df['Sequence']
output:
Name Sequence Merged
0 abc ghijklmkhf >abc\nghijklmkhf
1 bhf uyhbnfkkkkkk >bhf\nuyhbnfkkkkkk
2 dmf hjjfkkd >dmf\nhjjfkkd
Checking that there are two lines:
print(df.loc[0, 'Merged'])
>abc
ghijklmkhf
CodePudding user response:
As a complement to the mozway's solution, if you want to see the dataframe exactly with the format you mentioned, use the following:
from IPython.display import display, HTML
df["Merged"] = ">" df["Name"] "\n" df["Sequence"]
def pretty_print(df):
return display(HTML(df.to_html().replace("\\n","<br>")))
pretty_print(df)