老实说,我已经尝试了所有可能的解决方案,我想我已经快到了,但仍然有些问题不起作用 我有一个包含硬币名称及其标签的数据框。
硬币 | 标签 |
---|---|
比特币 | [可开采、pow、sha-256、价值存储、状态通道] |
我想提取二进制数据帧中的标签。就这样
硬币 | 可开采 | 沙256 | scrypt |
---|---|---|---|
比特币 | 1 | 1 | 0 |
狗狗币 | 1 | 0 | 1 |
我已经准备了一个这样的数据框
硬币 | 可开采 | 沙256 | scrypt |
---|---|---|---|
比特币 | 可开采 | 沙256 | scrypt |
狗狗币 | 可开采 | 沙256 | scrypt |
这个想法是,当我运行循环时,如果它找到列表中的标签,它会将其更改为 1,而当它没有找到时,它会离开它(甚至更好的是它会更改为 0)
for index_tags, row2 in tag_df2.iterrows():#final data set to be changed
for index, row in tags_head.iterrows():#dataset with the tags and the coin names
for my_tags in clean_set: #unique list of tags
if my_tags in (row['tags']):
print ('-----coin name-------------------->>>>',(row['name']))
print (my_tags)
tag_df2.loc[index_tags, my_tags]=1
现在它似乎可以迭代所有内容,但它只找到比特币的第一个值,并将相同的值复制到所有硬币。我也添加了一个指向我的 colab 笔记本的链接。 当我打印时,它似乎通过数据没有问题,但是当我尝试更新数据框时,它只是将一个复制到所有硬币。我希望有人可以帮助我。
https://colab.research.google.com/drive/1sn5lwqiNicoBy2L00EZNmhLgz_SBxsOg?usp=sharing
答案 0 :(得分:0)
您可以使用xCoords = [[16.8742 10.7265 30.0538 10.4524 12.6483 15.5349 10.2094 28.6425 9.2882]]
yCoords = [[14.5835 6.0766 12.7006 4.3638 5.0318 14.2657 8.3131 15.8346 6.1746]]
:
get_dummies
输出(此处仅显示前 3 列以说明结果):
# After you have generated `tags` DataFrame with
# tags = df_new[['name','tags']]
pd.get_dummies(tags.set_index('name')['tags'].explode()).sum(level=0)