Python Forum
How to clean image column in a dataframe using Python3.6?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to clean image column in a dataframe using Python3.6?
#1
Hi All,

I have fetched products data from a site and after normalization I store the result in a dataframe.
For a quick view of this df, following is the content of
print(df.head().to_dict())
:

{'Available': {0: 33, 1: 22, 2: 12, 3: 12, 4: 11}, 'Images': {0: ['https://example.com/e1e619ab5f11ffe311db03eefad5a2f4.jpg', 'https://example.com/7edc2e3cda8b63591bfacda9e254ad08.jpg', 'https://example.com/7ed2b44335f73cabe0411819820e4d0b.jpg', 'https://example.com/82fed0e56c531cde2fcf5b98f7418a6a.jpg', 'https://example.com/f536c423a97d0c9ab8c488a453818780.jpg', '', '', ''], 1: ['https://example.com/7d63597ae7a75b8481d9d4318951d6c1.jpg', '', '', '', '', '', '', ''], 2: ['https://example.com/7476c30281056d6810787c617fb4f30e.jpg', 'https://example.com/d59266704fa3f9750c02ea79956acf1e.jpg', '', '', '', '', '', ''], 3: ['https://example.com/7476c30281056d6810787c617fb4f30e.jpg', 'https://example.com/af285804c936cd3278cb2982b6f7a089.jpg', '', '', '', '', '', ''], 4: ['https://example.com/e4b6927a6bf8ad48394534c657ea0994.jpg', 'https://example.com/e630996c631e35013be0fbe0c0113fc5.jpg', '', '', '', '', '', '']}}
I need to clean the Images column here and want to store them without '','[',']' like below-
https://example.com/image1.jpg,https://e...image2.jpg in dataframe column.

I tried with below function-

def formatter(x):
    return ','.join(list(map(os.path.basename, x)))
df['Images'].apply(literal_eval).apply(formatter)
But it gives me ValueError: malformed node or string

Please help to resolve above issue.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 328 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 751 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Can i clean this code ? BSDevo 8 958 Oct-28-2023, 05:50 PM
Last Post: BSDevo
  Difference one column in a dataframe Scott 0 646 Feb-10-2023, 08:41 AM
Last Post: Scott
  splitting a Dataframe Column in two parts nafshar 2 966 Jan-30-2023, 01:19 PM
Last Post: nafshar
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 850 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  renaming the 0 column in a dataframe Led_Zeppelin 5 1,563 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 11,461 Jul-08-2022, 08:40 PM
Last Post: deanhystad
  Clean Up Script rotw121 2 1,022 May-25-2022, 03:24 PM
Last Post: rotw121
  Cannot convert the series to <class 'int'> when trying to create new dataframe column Mark17 3 8,552 Jan-20-2022, 05:15 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020