Ich versuche, Textdaten in Pandas DataFrame basierend auf bestimmten Tags und Werten in den Feldern einer anderen Spalte zu analysieren und sie in ihren eigenen Spalten zu speichern. Zum Beispiel, wenn ich diesen Datenrahmen, df erstellt:Neue Pandas-Spalten mit Regex-Analyse
df = pd.DataFrame([[1,2],['A: this is a value B: this is the b val C: and here is c.','A: and heres another a. C: and another c']])
df = df.T
df.columns = ['col1','col2']
df['tags'] = df['col2'].apply(lambda x: re.findall('(?:\s|)(\w*)(?::)',x))
all_tags = []
for val in df['tags']:
all_tags = all_tags + val
all_tags = list(set(all_tags))
for val in all_tags:
df[val] = ''
df:
col1 col2 tags A C B
0 1 A: this is a value B: this is the b val C: and... [A, B, C]
1 2 A: and heres another a. C: and another c [A, C]
Wie würde ich jeden der neuen „Tag“ Spalten mit ihren Werten von col2 bevölkern, so erhalte ich diese df:
col1 col2 tags \
0 1 A: this is a value B: this is the b val C: and... [A, B, C]
1 2 A: and heres another a. C: and another c [A, C]
A C B
0 this is a value and here is c. this is the b val
1 and heres another a. and another c