我已经从某个站点获取了产品数据,经过规范化后,我将结果存储在了数据框中。 为了快速浏览此df,以下是
的内容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', '', '', '', '', '', '']}}
我需要在此处清除“图像”列,并希望不使用“,'[',']'”来存储它们,如下所示- https://example.com/image1.jpg,https://e...image2.jpg
在数据框列中。
我尝试了以下功能-
def formatter(x):
return ','.join(list(map(os.path.basename, x)))
df['Images'].apply(literal_eval).apply(formatter)
但是它给我ValueError:格式错误的节点或字符串
请帮助解决以上问题。
答案 0 :(得分:0)
除非我误解了这个问题。我正在将以下内容应用于您上面的数据框。
def formatter(li):
return ",".join([x for x in li if x != ""])
df['Images'] = df['Images'].apply(formatter)
print(df)
Available Images
0 33 https://example.com/e1e619ab5f11ffe311db03eefa...
1 22 https://example.com/7d63597ae7a75b8481d9d43189...
2 12 https://example.com/7476c30281056d6810787c617f...
3 12 https://example.com/7476c30281056d6810787c617f...
4 11 https://example.com/e4b6927a6bf8ad48394534c657...
为了更好地查看其中之一:
print(df.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