[英]Python Data frame: If Column Name is contained in the String Row of Another Column Then 1 Otherwise 0
Column A 2C GAD D2 6F ABCDE
2C 1B D2 6F ABC 1 0 1 1 0
2C 1248 Bulers 1 0 0 0 0
Above is the dataframe I want to create. 上面是我要创建的数据框。
The first row represents the field names. 第一行代表字段名称。 The logic I want to employ is as follows: If the column name is in the "Column A" row, then 1 otherwise 0
我要采用的逻辑如下:如果列名在“ Column A”行中,则为1,否则为0
I have scoured Google looking for code answering a question similar to mine so I can test it out and backward engineer a solution. 我搜寻了Google,寻找可以回答类似于我的问题的代码,以便我可以对其进行测试并向后设计解决方案。 Unfortunately, I have not been able to find anything.
不幸的是,我找不到任何东西。
Otherwise I would post some code that I attempted to solve this problem but I literally have no clue. 否则,我会发布一些我试图解决此问题的代码,但实际上我毫无头绪。
You can use a list comprehension to create the desire data based on the columns and rows: 您可以使用列表推导根据列和行创建期望数据:
In [39]: row =['2C 1B D2 6F ABC', '2C 1248 Bulers']
In [40]: columns=['2C', 'GAD', 'D2', '6F', 'ABCDE']
In [41]: df = pd.DataFrame([[int(k in r) for k in columns] for r in row], index = ['2C 1B D2 6F ABC','2C 1248 Bulers'], columns=['2C', 'GAD', 'D2', '6F', 'ABCDE'])
In [42]: df
Out[42]:
2C GAD D2 6F ABCDE
2C 1B D2 6F ABC 1 0 1 1 0
2C 1248 Bulers 1 0 0 0 0
If you want a pure Pandas approach you can use pd.Series()
instead of list for preserving the columns and rows then use Series.apply
and Series.str.contains
to get the desire result: 如果您想使用纯Pandas方法,则可以使用
pd.Series()
代替list来保留列和行,然后使用Series.apply
和Series.str.contains
获得所需的结果:
In [73]: data = columns.apply(row.str.contains).astype(int).transpose()
In [74]: df = pd.DataFrame(data.values, index = ['2C 1B D2 6F ABC','2C 1248 Bulers'], columns=['2C', 'GAD', 'D2', '6F', 'ABCDE'])
In [75]: df
Out[75]:
2C GAD D2 6F ABCDE
2C 1B D2 6F ABC 1 0 1 1 0
2C 1248 Bulers 1 0 0 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.