私はPandas DataFrameのテキストデータを別のカラムのフィールドの特定のタグと値に基づいて解析し、それを独自のカラムに格納しようとしています。私はこのデータフレームを作成した場合、DF:私はこのDFを得るようRegex Parsingを使用した新しいPandasカラム
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]
にはどうすればCOL2からそれらの値を持つ新しい「タグ」列のそれぞれを移入します:
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