Home > front end >  Pandas remove brackets and comas from all of the cells in data frame
Pandas remove brackets and comas from all of the cells in data frame

Time:04-10

I have a problem as I can't remove brackets '(' ')' and comas ',' from my data frame

Data frame looks like this

{'Review_content': {0: ('Stood in line 2  years ago before a reveal of a car was released.  Not because I\'m a super Tesla "fan-boy", but the timing was right for when I\'d be ready for a new car.  I wanted the Model 3 to be an option and hopefully still qualify for the tax credit.  I\'ve been driving the car for more than a month now ... and WOW!  The acceleration and performance are beyond anything that I have experienced before.  The build is great.  Reviews of early release Model 3\'s should some issues but looks like those were corrected.  The single touch screen took a little getting used to, but now find it to be very intuitive.  Overall, a great car.\n\nUpdate after one year of ownership\n---------------------------------------------------\nCan I give the car six stars?  Absolutely love this car.  Everything about it.',),
  1: ('You gotta be able to embrace some change if you want to enjoy this car. Controls, buttons etc, or the lack thereoff, are vastly different from what you might be used to. But basically you can still drive the model 3 like any other car, except it’s whisper quiet and super fast. Runs like on rails, hugs the road like no other. It’s lighter, smaller, and perkier than the model S or X, but still feels rock solid. Has plenty of space for a family of four, and road trips are no problem with a range of 310 miles and the super charger network. Charging at home overnight is so much more convenient than getting gas. Car gets better every month with ‘over the air’ software updates, autopilot keeps evolving. Would not trade the model 3 for any other car in the world! :)',),
  2: ('the three best pieces of machinery in my lifetime is the boeing b52 stratofortress, the apple ip[hone and the tesla model 3!',),
  3: ('My Tesla 3 has the extended battery, premium packages, 19" wheels.    10,000mi review.                                                                                                                                                        What a fun car to drive, very fast, glued to the ground, corners like crazy, This is a sporty car by design, so the ride is firm but very good. The computer screen requires some patience to learn where everything is.....but after a few days, I like it.     The navigation is superb, the sound is quite good, I downloaded the Tesla app on my iPhone, now the car locks/unlocks when getting in the car or leaving, you can set the temperature remotely, the garage door opens within 100\', closes when leaving,  storage is adequate  for this size of a car. It charges automatically after midnight for the lowest electrical rate.  I commute 100 miles so I don\'t have to use superchargers which are slow and pricey.  There is a lot of noise coming through on bad roads, can\'t wait to try some other tires, also significant amount of wind noise. I don\'t buy the Tesla story that since you don\'t have an engine, your hear more noise, simply poor design....I am sure the upcoming electric cars for the big boys will be a lot smoother. Since all the initial Tesla 3 are identical except for the color and tire size, I found a company that sells many options to make it yours (RPMTESLA).\nWell 27000 miles stills works like a charm, no problem to report. I did buy a soundproofing kit, seems to help. I have purchased a set of Vredestein Quatrac 5, they have fantastic reviews\nI have 48000 mi now, no loss in range or speed, a great little car!',),
  4: ('Tesla hit it out of the park with the Model 3 4-door sedan.  As beautiful as the car is on the outside, it is an absolute joy to drive.  Even my wife, a non-techie, loves driving the car.  Virtually all the adjustments to climate control, seats, mirrors, steering wheel and other options are via the very well organized centrally mounted touchscreen.  Though smaller than the Model S sedan with a smallish 75 kWh battery pack, the Model 3 has plenty of leg room for 6 footers.  Tesla says 0-60 in 5.1 seconds.  It is more like 4.6.  Tesla rates the range at 310 miles.  The EPA says 334.  It is a great car on a road trip because of the efficiency with which it uses electricity from the battery pack and the rapidity of charging since there is less energy to replace. You will have trouble buying and eating a hamburger in the time it needs to charge, especially since they modified the charging software to shorten the charging time.\n\nAround town it is very agile in traffic and parks itself, if you like, parallel and perpendicular.  On the road the Traffic Aware cruise control and auto steering are easy to use and work pretty well, though the driver must be alert at all times. \n\nNow with over 10,000 miles under its belt, the car has gotten better via over the air software updates.  The navigation system has been improved.  The car now can navigate itself off an Interstate exit, if you so choose.  The climate control operation is more intuitive and the touchscreen interface has even more flexibility.  All at no added cost.\n\nTesla decided they had to increase the processing power of the main computer in the car and add redundancy for extra reliability.  They changed our our computer board during a one day visit to their local Service Center.  The car now recognizes stop lights and stop signs to help navigate on local roads in addition to interstates.  Actual self driving is a ways off but the driver assistance features come in handy, especially on cross country trips.',)},
 'Review_date': {0: ('06/29/2018 updated 07/02/2019Long Range 4dr Sedan (electric DD)',),
  1: ('04/24/2018 Long Range 4dr Sedan (electric 1DD)',),
  2: ('07/01/2018 Long Range 4dr Sedan (electric 1DD)',),
  3: ('07/08/2018 updated 01/11/2021Long Range 4dr Sedan (electric DD)',),
  4: ('06/08/2018 updated 06/15/2020Long Range 4dr Sedan (electric DD)',)},
 'Review_overall_rating': {0: ('5 out of 5 stars',),
  1: ('5 out of 5 stars',),
  2: ('5 out of 5 stars',),
  3: ('5 out of 5 stars',),
  4: ('5 out of 5 stars',)},
 'Review_title': {0: ('Worth the Wait',),
  1: ('So happy to own and drive the best car ever',),
  2: ('old guy with a new toy',),
  3: ('Fantastic value, still is after 2 years and 34K mi',),
  4: ('Model 3 - still a winner in every respect',)},
 'Reviewer_name': {0: ('Paul',),
  1: ('Namafe',),
  2: ('Don Lockard',),
  3: ('Pierre Monette',),
  4: ('George Hawley',)}}

I would like it to look like this:

Exemplary first entry

Reviewer_name   Review_date     Review_overall_rating   Review_title    Review_content
0   Paul    06/29/2018 updated 07/02/2019Long Range 4dr S...    5 out of 5 stars    Worth the Wait  Stood in line 2  years ago before a reveal of...

CodePudding user response:

You have tuples, so one option is to explode it:

out = df.explode(df.columns.tolist()).reindex(['Reviewer_name','Review_date','Review_overall_rating','Review_title','Review_content'], axis=1)

Output:

    Reviewer_name                                        Review_date  Review_overall_rating                                       Review_title                                     Review_content
0            Paul  06/29/2018 updated 07/02/2019Long Range 4dr Se...       5 out of 5 stars                                     Worth the Wait  Stood in line 2  years ago before a reveal of ...
1          Namafe     04/24/2018 Long Range 4dr Sedan (electric 1DD)       5 out of 5 stars        So happy to own and drive the best car ever  You gotta be able to embrace some change if yo...
2     Don Lockard     07/01/2018 Long Range 4dr Sedan (electric 1DD)       5 out of 5 stars                             old guy with a new toy  the three best pieces of machinery in my lifet...
3  Pierre Monette  07/08/2018 updated 01/11/2021Long Range 4dr Se...       5 out of 5 stars  Fantastic value, still is after 2 years and 34...  My Tesla 3 has the extended battery, premium p...
4   George Hawley  06/08/2018 updated 06/15/2020Long Range 4dr Se...       5 out of 5 stars          Model 3 - still a winner in every respect  Tesla hit it out of the park with the Model 3 ...

CodePudding user response:

Using regular expression to replace the brackets and commas :

import pandas as pd
# create df from the dictionary given.
df.apply(lambda x : x.astype(str).str.replace(r"(\(|\)|\,)","", regex = True))
  • Related