简体   繁体   English

Python数据框:如果列名称包含在另一列的字符串行中,则为1,否则为0

[英]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.applySeries.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.

相关问题 Python数据框:根据数据框另一列的字符串行中是否包含列名,将行填充为1或0 - Python Dataframe: Fill in Row as 1 or 0 Based on If Column Name is Contained in String Row of Another Column in Dataframe 如果与另一个数据框列匹配,则取消一个数据框列中包含的匹配字符串 - Nullify the matched string contained in one Data frame column if that is a match with another Data frame column 如何根据行/列名称将一个数据框的列附加为另一个数据框的行? - How to append column of one data frame as row of another data frame based on row/column name? Python Pandas:检查一列中的字符串是否包含在同一行中另一列的字符串中 - Python Pandas: Check if string in one column is contained in string of another column in the same row Pandas Python-如何将数组(与数据框的长度不同)转换为数据框,并保留行名和列名? - pandas python - how to convert array (different length with data frame) to data frame and keep the row and column name? 我需要基于列名从一个数据框到另一个数据框的值在 python pandas 中 - I need values from one data frame to another data frame in python pandas based on column name 根据 Python 中另一个数据框的另一列输入列值 - Enter column value based on another column of another data frame in Python 如何从另一个数据框中获取值的行列名称 - How to get the row-column name of a value from another data frame Python - 如果列名包含特定字符串,则更改该列中的值,否则值保持不变 - Python - If column name contains a specific string then change the values in that column otherwise the values remain 通过在熊猫的另一列中拆分字符串来创建新的数据框列 - Creating a new data frame column, by splitting a string in another column in pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM