1

I have a DF as shown below

DF =
Index   R
0       A
1       "B,C"
2       D
3       "E,F"

I want to remove all "" from the values:

DF_New =
Index   R
0       A
1       B,C
2       D
3       E,F

Using join(literal_eval(x)) or other constructions don't work. What should I do?

Mi.
  • 510
  • 1
  • 4
  • 20

2 Answers2

1

You can use the .str accessor:

>>> df['R'].str.replace('"', '')
0      A
1    B,C
2      D
3    E,F
Name: R, dtype: object

which should eliminate all literal double-quotes from that column.

Note that each result will be a str such as 'B,C'. If you want to convert each to a list container, use:

>>> df['R'].str.replace('"', '').str.split()
0      [A]
1    [B,C]
2      [D]
3    [E,F]
Name: R, dtype: object
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
  • Eh, this was the first thing I tried and it didn't do a thing. I mean even this did nothing ".replace('^"', '', regex=True).replace('"$', '', regex=True)" – Mi. Aug 19 '18 at 15:37
0

You could use:

DF.loc[DF['R'].str.startswith('"'), 'R'] = DF['R'].str[1:]
DF.loc[DF['R'].str.endswith('"'), 'R'] = DF['R'].str[:-1]

For example,

In [5]: DF
Out[5]: 
   Index      R
0      0      A
1      1  "B,C"
2      2      D
3      3  "E,F"

In [6]: DF.loc[DF['R'].str.startswith('"'), 'R'] = DF['R'].str[1:]
In [7]: DF.loc[DF['R'].str.endswith('"'), 'R'] = DF['R'].str[:-1]
In [8]: DF
Out[8]: 
   Index    R
0      0    A
1      1  B,C
2      2    D
3      3  E,F

Note that this will only replace double quotes that appear at the beginning or end of the string, not in the middle.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677