【发布时间】:2019-11-22 09:21:16
【问题描述】:
我是 python 和 pandas 的新手。在这里,我有一个以下数据框。
did features offset word JAPE_feature manual_feature
0 200 0 aa 200 200
0 200 11 bf 200 200
0 200 12 vf 100 100
0 100 13 rw 2200 2200
0 100 14 asd 2600 100
0 2200 16 dsdd 2200 2200
0 2600 18 wd 2200 2600
0 2600 20 wsw 2600 2600
0 4600 21 sd 4600 4600
现在,我有一个数组,其中包含该 id 可以出现的所有特征值。
feat = [100,200,2200,2600,156,162,4600,100]
现在,我正在尝试创建一个看起来像的数据框,
id Features
100 200 2200 2600 156 162 4600 100
0 0 1 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0
2 0 1 0 0 0 0 0 0
3 0 1 0 0 0 0 0 0
4 1 0 0 0 0 0 0 0
5 1 0 0 0 0 0 0 0
7 0 0 1 0 0 0 0 0
8 0 0 0 1 0 0 0 0
9 0 0 0 1 0 0 0 0
10 0 0 0 0 0 0 1 0
所以,在做比较的时候,
feature_manual
1
1
0
0
1
1
1
1
1
Here compairing the features and the manual_feature columns. if values are same then 1 or else 0. so 200 and 200 for 0 is same in both so 1
所以,这是预期的输出。在这里,我尝试在新的 csv 中为该功能添加值 1,并为其他 0 添加值。
So, it is by row by row.
所以,如果我们检查第一行的特征是 200,那么在 200 处有 1,其他都是 0。
谁能帮我解决这个问题?
我试过的是
mux = pd.MultiIndex.from_product([['features'],feat)
df = pd.DataFrame(data, columns=mux)
所以,这里创建子列但删除所有其他值。谁能帮帮我?
【问题讨论】:
-
到目前为止你尝试了什么?
-
实际上我尝试使用 for 循环并创建子列.. 但它不起作用。
-
请包含该代码并显示您从中获得的输出,以便人们更容易回答您的问题
-
添加了我试过的请检查
标签: python python-3.x pandas numpy