请帮助,我需要创建一个新列。如果我的列类型(列表)包含值。你知道吗
从查找表返回相应的值。 我尝试了很多方法,对python还是陌生的,希望我能接近
dh是下面的数据帧
sub-genre first second mid-genre genre
indie indie Alternative rock
dream pop dream pop Alternative rock
shoegaze shoegaze Alternative rock
post-hardcore post hardcore HardcorePunk rock
emo emo HardcorePunk rock
screamo screamo HardcorePunk rock
synthcore synthcore Harcore Punk rock
rock rock Contemporary rock
diy=下面的数据帧
artist genres New Column
2:54 ['metropopolis'] No Genre (blank)
22 ['norwegian rock'] Contemporary
27 ['boston rock'] Contemporary
33 [] No Genre (blank)
36 ['ambient', 'compositional ambient', 'drift', ...
44 ['emo', 'pop punk', 'skate punk'] Hardcore Punk
52 []
68 []
83 ['hip hop quebecois'] Hip hop
下面的代码尝试
diy = pd.DataFrame(data[['artist','genres']])
for i in diy['genres'].iteritems():
for x, y, z, t in zip(dh['first'], dh['second'],dh['mid-genre'],dh['genre']):
if h.str.contains(x) and h.str.contains(z):
diy['mid-genre'] = z
diy['Main-genre'] = t
错误消息
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
第二次尝试时,我在IF语句中添加了.any(),以尝试处理异常:
if h.str.contains(x).any() and h.str.contains(z).any():
UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
我想出了解决办法,以防其他人有类似的任务。你知道吗
相关问题 更多 >
编程相关推荐