Row-wise concatenation in pandas
Question:
Answers:
If I understand you right you can use list-comprehension with str.join
for the task:
out = [" ".join(df[c]) for c in df]
print(out)
Prints:
['Brand Model', 'RIVIAN R1T', 'LUCID Air Dream Edition']
DataFrame used:
Col1 Col2 Col3
Index
3 Brand RIVIAN LUCID
4 Model R1T Air Dream Edition
EDIT: To append the row:
df.loc[df.index.max() + 1] = [" ".join(df[c]) for c in df]
print(df)
Prints:
Col1 Col2 Col3
Index
3 Brand RIVIAN LUCID
4 Model R1T Air Dream Edition
5 Brand Model RIVIAN R1T LUCID Air Dream Edition
If I understand you right you can use list-comprehension with str.join
for the task:
out = [" ".join(df[c]) for c in df]
print(out)
Prints:
['Brand Model', 'RIVIAN R1T', 'LUCID Air Dream Edition']
DataFrame used:
Col1 Col2 Col3
Index
3 Brand RIVIAN LUCID
4 Model R1T Air Dream Edition
EDIT: To append the row:
df.loc[df.index.max() + 1] = [" ".join(df[c]) for c in df]
print(df)
Prints:
Col1 Col2 Col3
Index
3 Brand RIVIAN LUCID
4 Model R1T Air Dream Edition
5 Brand Model RIVIAN R1T LUCID Air Dream Edition