仅将列表长度等于1的行转换为字符串

我在Python中有一个DataFrame,其中h3_to_multi_polygon列中的每一行都是一个列表:

tags

我只想将具有列表df >>> name tags >>> alice | [a] >>> bruce | [a,b,c] 的行转换为字符串。 预期结果

length = 1
lcq3822987 回答:仅将列表长度等于1的行转换为字符串

您可以使用:

c=df['tags'].str.len().eq(1)
df['tags']=np.where(c,df['tags'].str[0],df['tags'])
print(df) #df.to_csv('file.txt',sep='|',index=False)

    name          tags
0  alice             a
1  bruce     [a,b,c]
本文链接:https://www.f2er.com/3142650.html

大家都在问