※ かなりテキトーな回答
python
1import pandas as pd
2from itertools import combinations
3from difflib import SequenceMatcher
4
5pd.set_option('display.unicode.east_asian_width', True)
6
7df = pd.DataFrame({
8 'c1': ['ごはんパン', 'パンごはん', 'めんパン', 'めんごはん', 'ごはんめん']
9})
10
11common = []
12for s1, s2 in combinations(df['c1'], 2):
13 m = SequenceMatcher(None, s1, s2).find_longest_match()
14 if m.size > 0:
15 common.append(s1[m.a:m.a+m.size])
16
17common = {*common}
18common = [i for i in common if all(c not in i for c in common if i != c)]
19
20dfx = (
21 df['c1'].str.extractall(f'({"|".join(common)})')
22 .unstack().droplevel(0, axis=1)
23 .rename_axis(columns=None))
24dfx.columns = ['c2', 'c3']
25dfx = pd.concat([df, dfx], axis=1)
26
27print(dfx.to_markdown(index=False))
c1 | c2 | c3 |
---|
ごはんパン | ごはん | パン |
パンごはん | パン | ごはん |
めんパン | めん | パン |
めんごはん | めん | ごはん |
ごはんめん | ごはん | めん |