Skip to content
Advertisement

Method type inside a column pandas

I have the following data frame

{'title': {4677: 'Lego City 2020 - Rumours, Speculation and Discussion',
  2397: 'Lego City 2019 - Rumours, Speculation and Discussion',
  4171: 'Lego City 2019 - Rumours, Speculation and Discussion',
  628: 'Lego City 2018 - Rumours, Speculation and Discussion',
  6930: 'Lego City 2020 - Rumours, Speculation and Discussion'},
 'category': {4677: 'LEGO Town',
  2397: 'LEGO Town',
  4171: 'LEGO Town',
  628: 'LEGO Town',
  6930: 'LEGO Town'},
 'user': {4677: 'Vilhelm22',
  2397: 'Klaus-Dieter',
  4171: 'Aanchir',
  628: 'Fenghuang0296',
  6930: 'Klaus-Dieter'},
 'score': {4677: 513, 2397: 3775, 4171: 10931, 628: 2389, 6930: 3775},
 'content': {4677: 'We’re owed a cargo harbour so much. I find it strange that TLGuse 10x6x6 containers in 60169 Cargo Terminal and 60198 Cargo Train, and yet there is no harbour. The 10x6x6 containers were last seen before that in 2007’s LEGO City Harbour 7994 and Container Stacker 7992. There’s only been one harbour since then, in 2011.,I also cannot think of a single harbour for people rather than cargo in the entire CITY subtheme, so 16 years or so. Basically, we’re owed a load of harbour stuff in my opinion.',
  2397: "Thanks to Hothbricks we finally have got now a nearly complete list of the winter wave sets:,(The French set names are the original ones - if unsure about my translation into English, I put them in brackets. My comments are written italic.),60206 Sky Police Jet Patrol - $ 9,99 (54 parts),60207 Sky Police Drone Chase - $ 29,99 (192 parts),60208 Sky Police Parachute Arrest - $ 39,99 (218 parts),60209 Sky Police Diamond Heist - $59.99 (400 parts),60210 Sky Police Air Base - $89,99 (529 parts),60211 ??? - ,I bet for a Sky Police Exclusive,,60212 Barbecue Burn Out - $9.99 - (64 parts),60213 Fire At The Harbor ,or, Train Station,?, (,L’Incendie sur Le Quai, - 97 parts) - ,I bet 20 Euros,,60214 Burger Bar Fire Rescue - $39.99 (327 parts),60215 Fire Station (509 parts) - ,I bet 50 Euros,,60216 Downtown Firefighters (,Les Pompiers du Centre-Ville, - 943 parts) - ,I bet 100 Euros,,60217 ??? - ,I bet for a Firefighter Exclusive,,60218 Desert Rallye Car (75 parts) - ,I bet 10 Euros,,60219 Wheel Loader (88 parts) - ,I bet 20 Euros,,60220 Garbage Truck - $19.99,60221 Diving Yacht - $19.99 (148 parts),60222 Pisten Bully (197 parts) - ,I bet 20 Euros,,60223 Field Chopper Transport (,Le Transport de l'ensileuse, - 358 parts) - ,I bet 40 Euros,,60234 Kajak Adventure (84 parts) - ,I bet 10 Euros,,60236 Straight and T-junction - ,I bet 15 Dollars,,60237 Curve and Crossroad - $14.99,60239 Police Patrol Car (92 parts) - ,I bet 10-20 Euros,,_______________________________________________________________________________,Personally I'm glad that we'll get a pisten bully and a field chopper! ,Plus I'm really curious about what set 60216 will inculde: for sure some fire engines, but hopefully even one or two burning houses.",
  4171: 'I guess maybe it only shows up in alternate photos for some of the sets and not the main photos? Or maybe it's just not in any of the pics they chose to upload? Or maybe it's just that now that we're 5 months into the year, a lot of the sets it has appeared in are no longer featured in the Shop site's "New" category.,In any case, it appears in these 9 sets according to Brickset (which pulls its inventories from the LEGO Customer Service site): ,https://brickset.com/sets/containing-part-6254100,,Besides the rocket transport, other upcoming sets for which we've seen official pics showing this brick separator color include ,Hogwarts Clock Tower,, ,Dual Rotor Helicopter,, ,Heartlake City Fairground Pier,, ,Heartlake City Restaurant,, ,Rescue Center Lighthouse,, ,Rescue Boat,, ,Deep Space Rocket and Launch Control,, and ,Castle of the Forsaken Emperor,. I'm sure we'll see even more of it as time goes on!',
  628: "Do you know where I can find those?,Aww, the police station will be built into rock? That doesn't sound promising . . I was hoping that the Mountain Police Station might, with a few changes and some extra snow, be put into my Winter Village as the local arm of the law (yes. I was excited about a police station. You may expect the apocalypse now.), but if it's literally built into the side of a mountain . . . that'ssounds not very promising. :( But, on the bright side, the world isn't ending.",
  6930: "Now to the Great Vehicles:,All in all I have to say that TLG keeps the high standard of this City subtheme - which I really appreciate:,The ,Monster Truck, looks very simple and boring. ,The ,Racing Boat Transport, is more or less the same like the version of 2011 - the towing vehicle looks a bit better, but the boat itself much worse -, so really boring. And a lost opportunity to give us a much more attractive and even since long or even never seen before boat like a push boat, a small harbor tour boat, a sailing yacht etc. ,The ,Ice-Cream Truck, looks great from the exterior and I highly welcome a new foodtruck. Unluckily the interior is absolutely weird with having several tiles in which the ice-cream pieces can be clipped - a huge let-down. Instead I would have liked to see one big counter in which all ice-cream pieces are - like it is in real life. ,The ,Postal Plane, is a very nice set - simple, but functional and nice looking. ,The ,Street Sweeper, looks so cute! ,The ,Safari Truck, is simply aweseome and imo the best set of this series! The colors and desgin are perfect and that the lion woman comes with it is great, too. ,I don't comment on the bulldozer set since it's a Juniors and not a City set.,Now to the Racing sets:,The ,Stunt Team, is a simple but nice set. ,On the one hand I agree with those who say that the ,Gas Station, is too small. On the other hand its price is much lower than the one of the past one. Plus in principle everything needed is included and looks very well desigend. Plus there are two completely new cars: the SUV and the minibus which both look excellent. So all in all I'm very content with this set. ,The ,Racing Cars, look very well designed! Plus I like the fact that one is an electric car (although you can argue that racing polutes our climate massively - so it's no matter whether you use an e-motor or a regular motor). ,The best set of this series and imo of the whole 1HY City wave at all is of course the ,Tuning Workshop, which overbids my exceptions by far! There are so many different and amazing looking vehicles included, the caravan (altough its interior is regrettably a let-down because a table and a second chair are missing), the dog's hut and all that acessory make it looking absolutely realistic. Not to speak about the building itself with is imo a masterpiece: awesome and absolutely realisitc exterior as well as a very detailed interior (although it's bad that because of th 2x4 tiles in front of the working space you can't fix a minifigure there). ,I really hope that the summer sets will have the same standard like the Racing sets!"},
 'date': {4677: '2019-06-30',
  2397: '2018-09-13',
  4171: '2019-05-03',
  628: '2017-11-01',
  6930: '2019-12-09'},
 'location': {4677: 'United Kingdom',
  2397: 'Germany',
  4171: 'Canada',
  628: 'Australia',
  6930: 'Germany'},
 'day': {4677: 30, 2397: 13, 4171: 3, 628: 1, 6930: 9},
 'month': {4677: 6, 2397: 9, 4171: 5, 628: 11, 6930: 12},
 'year': {4677: 2019, 2397: 2018, 4171: 2019, 628: 2017, 6930: 2019},
 'sentence': {4677: 'I find it strange that TLGuse 10x6x6 containers in 60169 Cargo Terminal and 60198 Cargo Train, and yet there is no harbour.',
  2397: '- ,I bet for a Sky Police Exclusive,,60212 Barbecue Burn Out - $9.99 - (64 parts),60213 Fire At The Harbor ,or, Train Station,?, (,L’Incendie sur Le Quai, - 97 parts) - ,I bet 20 Euros,,60214 Burger Bar Fire Rescue - $39.99 (327 parts),60215 Fire Station (509 parts) - ,I bet 50 Euros,,60216 Downtown Firefighters (,Les Pompiers du Centre-Ville, - 943 parts) - ,I bet 100 Euros,,60217 ???',
  4171: 'Or maybe it's just that now that we're 5 months into the year, a lot of the sets it has appeared in are no longer featured in the Shop site's "New" category.,In any case, it appears in these 9 sets according to Brickset (which pulls its inventories from the LEGO Customer Service site): ,https://brickset.com/sets/containing-part-6254100,,Besides the rocket transport, other upcoming sets for which we've seen official pics showing this brick separator color include ,Hogwarts Clock Tower,, ,Dual Rotor Helicopter,, ,Heartlake City Fairground Pier,, ,Heartlake City Restaurant,, ,Rescue Center Lighthouse,, ,Rescue Boat,, ,Deep Space Rocket and Launch Control,, and ,Castle of the Forsaken Emperor,.',
  628: 'I was hoping that the Mountain Police Station might, with a few changes and some extra snow, be put into my Winter Village as the local arm of the law (yes.',
  6930: ",I don't comment on the bulldozer set since it's a Juniors and not a City set.,Now to the Racing sets:,The ,Stunt Team, is a simple but nice set."},
 'pos_sentence_sentiment': {4677: 0.0,
  2397: 0.044,
  4171: 0.061,
  628: 0.159,
  6930: 0.121},
 'neg_sentence_sentiment': {4677: 0.167,
  2397: 0.11,
  4171: 0.02,
  628: 0.0,
  6930: 0.0},
 'neu_sentence_sentiment': {4677: 0.833,
  2397: 0.846,
  4171: 0.918,
  628: 0.841,
  6930: 0.879},
 'vader_comp_sentence_sentiment': {4677: -0.4588,
  2397: -0.594,
  4171: 0.6597,
  628: 0.6705,
  6930: 0.5719},
 'comp_sentence_sentiment': {4677: -0.9999999999999999,
  2397: -0.42857142857142855,
  4171: 0.5000000000000002,
  628: 0.9999999999999998,
  6930: 1.0},
 'product': {4677: ['Cargo Terminal', 'Cargo Train'],
  2397: ['Barbecue Burn Out',
   'Train Station',
   'Burger Bar Fire Rescue',
   'Fire Station'],
  4171: ['Deep Space Rocket and Launch Control'],
  628: ['Police Station'],
  6930: ['Stunt Team']}}

I need to iterate over the product to extract the unique values.
The problem is that when I tried to iterate over the column product:

for i in c.product:
    print(i)

The following error is shown: “‘method’ object is not iterable”.
To extract the product from the sentences I have used the code (using str.findall to retrieve the exact match from dictionary) which does not give any error when I apply on other examples.
Any suggestion on how to solve the problem? So that I can iterate over the column and lists?

Advertisement

Answer

In pandas exist function DataFrame.product, so selecting c.product raise error.

If need unique values in list use:

s = df['product'].explode().unique().tolist()
print (s)
['Cargo Terminal', 'Cargo Train', 'Barbecue Burn Out', 'Train Station', 'Burger Bar Fire Rescue', 'Fire Station', 'Deep Space Rocket and Launch Control', 'Police Station', 'Stunt Team']

If need iteration by multiple columns use zip:

for i, j in zip(df['product'], df['comp_sentence_sentiment']):
    print (i, j)
    
['Cargo Terminal', 'Cargo Train'] -0.9999999999999999
['Barbecue Burn Out', 'Train Station', 'Burger Bar Fire Rescue', 'Fire Station'] -0.42857142857142855
['Deep Space Rocket and Launch Control'] 0.5000000000000002
['Police Station'] 0.9999999999999998
['Stunt Team'] 1.0

And for unique lists use sets:

for i, j in zip(df['product'], df['comp_sentence_sentiment']):
    print (set(i), j)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement