0

I have dataframe df that has 3 columns

RowID   Col1   Col2
--------------------
1       Cat    Dog
2       Bird   Red
3       Vic    Nile
4       Drive  Job
5       NIce   BB

I want to have a 4th column called CSV

df["CSV"] = df["RowID"] + "," + df["Col1"] + "," + df["Col2"]

but this is not working

how to do that to get my dataframe look like this

RowID   Col1   Col2   CSV
-------------------------
1       Cat    Dog    1,Cat,Dog
2       Bird   Red    2,Bird,Red
3       Vic    Nile   3,Vic,Nile
4       Drive  Job    4,Drive,Job
5       NIce   BB     5,NIce,BB
asmgx
  • 7,328
  • 15
  • 82
  • 143

3 Answers3

0

Convert df to a numpy array, and using a list comprehension, format each row to string:

df['RowID'] = df['RowID'].astype(str)
df['CSV'] = [','.join(lst) for lst in df.to_numpy()]

Another option is, convert apply join across columns:

df['CSV'] = df.apply(','.join, axis=1)

Output:

   RowID   Col1  Col2          CSV
0      1    Cat   Dog    1,Cat,Dog
1      2   Bird   Red   2,Bird,Red
2      3    Vic  Nile   3,Vic,Nile
3      4  Drive   Job  4,Drive,Job
4      5   NIce    BB    5,NIce,BB
0

Your method was almost correct (and efficient). You didn't provide an error message, but probably a type error. You would need to convert the first column to string:

df["CSV"] = df["RowID"].astype(str) + "," + df["Col1"] + "," + df["Col2"]
mozway
  • 194,879
  • 13
  • 39
  • 75
0

aggregate df row wise. That will give you a list. Then drop corner brackects

df['new']=df.agg(list,1).astype(str).replace('[^\w\,]','', regex=True)

      RowID   Col1  Col2          new
0      1    Cat   Dog    1,Cat,Dog
1      2   Bird   Red   2,Bird,Red
2      3    Vic  Nile   3,Vic,Nile
3      4  Drive   Job  4,Drive,Job
4      5   NIce    BB    5,NIce,BB
wwnde
  • 26,119
  • 6
  • 18
  • 32