Возможно, есть лучший способ, но он будет делать то, что вам нужно:
import pandas as pd
import numpy as np
pattern = ['a,b,c', 'b', 'c,b', 'c']
nb = [150, 100, 30, 10]
df = pd.DataFrame(data=np.column_stack([pattern, nb]), columns=['pattern', 'nb'])
df
>>> pattern nb
0 a,b,c 150
1 b 100
2 c,b 30
3 c 10
А затем вы можете проверить значения, добавить правильное значение в список и затем добавить в DataFrame на конец:
# we want to check whether a, b, or c is in the original pattern
# so we loop over a, b, and c one at a time
for value in ['a', 'b', 'c']:
# when we do our check we want to store the values
# so we initialise an empty list that we will use to add the values toused
new = []
# now we loop over each pattern in the original DataFrame
# enumerate is gives us back an index 'i' and a value 'p' ('p' for pattern in this case)
# just like normal for loop
# we need the index 'i' later to access the DataFrame values
for i, p in enumerate(df['pattern']):
# we now do a test to see if value (ie. a, b, or c) is in 'p'
if value in p:
# if it is we get the value of the pattern from the original DataFrame -> df['nb'].iloc[I]
# df['nb'] selects the column in the DataFrame
# and .iloc[i] gets the correct row
# and we add it to the list
new.append(df['nb'].iloc[i])
else:
# if a, b, or c is not in the pattern we add 0 to the list
new.append(0)
# after one iteration of the loop (a, b, c) and all tests
# we then add a new column to the DataFrame
# value in this case is 'a', 'b', or 'c'
# so the column names are 'a', 'b' or 'c'
df[value] = new
df
>>> pattern nb a b c
0 a,b,c 150 150 150 150
1 b 100 0 100 0
2 c,b 30 0 30 30
3 c 10 0 0 10