-2

I want to get values out of quotes only. The rest should be NaN.

I have dataframe like this:

Col A                 Col B            Col C
b'Ford'               b'VW'              b''
b''                   b'VW'              b''
b''                   b''                b'BMW'
B'Ford'               b''                b''

and i want output like this:

desired output:

Col A                 Col B            Col C
Ford                    VW              NaN 
NaN                     VW              NaN 
NaN                     NaN             BMW
Ford                    NaN             NaN 
s_khan92
  • 969
  • 8
  • 21
  • Just check how to convert bytes to string in pandas. https://stackoverflow.com/questions/40389764/how-to-translate-bytes-objects-into-literal-strings-in-pandas-dataframe-pytho – Kishan Mehta Mar 16 '20 at 13:00
  • Does this answer your question? [How to translate "bytes" objects into literal strings in pandas Dataframe, Python3.x?](https://stackoverflow.com/questions/40389764/how-to-translate-bytes-objects-into-literal-strings-in-pandas-dataframe-pytho) – Kishan Mehta Mar 16 '20 at 13:00
  • @jezrael can you help? I tried already other answer but nothing worked. – s_khan92 Mar 21 '20 at 12:29

1 Answers1

1

Not sure why you have those strings as bytes... But one way is with DataFrame.applymap and decoding all bytes with bytes.decode:

df.applymap(bytes.decode)

    Col A Col B Col C
0  Ford    VW      
1          VW      
2               BMW
3  Ford            

If you want those empty strings as NaN:

df.applymap(bytes.decode).replace('', float('nan'))

   Col A Col B Col C
0  Ford    VW   NaN
1   NaN    VW   NaN
2   NaN   NaN   BMW
3  Ford   NaN   NaN
yatu
  • 86,083
  • 12
  • 84
  • 139