简体   繁体   中英

replace pandas DataFrame value pattern

This is my current DataFrame using pandas, and it has some mixed type values in order_number column

       order_number             created_time  customer_id  driver_id 
153280        40487  2017-02-01 12:39:25.887         1413       96.0   
153281       118898  2017-02-01 10:52:38.822        51640     5382.0   
153282      "36968"  2017-02-02 20:54:43.141        49072     6851.0   
153283      "68383"   2017-02-02 19:01:08.52        28742     4479.0   
153284      "56261"  2017-02-01 06:09:53.245        31656        NaN 

and I want to remove the quote mark from the order number so that the DataFrame would be like this:

       order_number             created_time  customer_id  driver_id 
153280        40487  2017-02-01 12:39:25.887         1413       96.0   
153281       118898  2017-02-01 10:52:38.822        51640     5382.0   
153282        36968  2017-02-02 20:54:43.141        49072     6851.0   
153283        68383   2017-02-02 19:01:08.52        28742     4479.0   
153284        56261  2017-02-01 06:09:53.245        31656        NaN 

I already tried to use replace method like below but it didn't work.

df['order_number'].replace('""','')

Can anyone help? Any suggestion would be appreciated :)

I think you need str.strip :

df['order_number'] = df['order_number'].str.strip('"').astype(float)

Or add parameter regex=True for replace substring s:

df['order_number'] = df['order_number'].replace('"','', regex=True).astype(float)

Last convert values to float s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM