0

I have a data frame like this -

a~~b~~c~~d
e~~f~~g~~h
i~~j~~k~~l
m~~n

I want to explode the rows into columns at "~~" with first row split as the column names. Data is large and there are some rows where length of string is not same i.e there may be only two "~~" in a row. In that case I want NaN on the columns. I have tried -

df[colname].str.split("~~", 1, expand = True) But I am getting error - AttributeError: 'DataFrame' object has no attribute 'str'

Expected output -

a   b   c   d
e   f   g   h
i   j   k   l
m   n   NaN   NaN
Mohammad Amir
  • 133
  • 1
  • 6
  • It means there are duplicated columns names, so `print (df[colname])` return not one column, but all columns. So need deduplicate columns names. – jezrael Dec 09 '20 at 10:41
  • @jezrael I dont understand. Lets say my colname is "a~~b~~c~~d". What should I do? – Mohammad Amir Dec 09 '20 at 10:45
  • 1
    it means there are `col1, col1, col2`, so if use `print(df['col1'])` it return 2 columns, not one – jezrael Dec 09 '20 at 10:46

0 Answers0