繁体   English   中英

如何使用Python3.6清理数据框中的图像列?

[英]How to clean image column in a dataframe using Python3.6?

我已经从站点获取了产品数据,标准化后将结果存储在数据框中。 为了快速浏览此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:格式错误的节点或字符串

请帮助解决上述问题。

除非我误解了这个问题。 我正在将以下内容应用于您上面的数据框。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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