简体   繁体   中英

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

I have scoured Google looking for code answering a question similar to mine so I can test it out and backward engineer a solution. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM