I'm trying to build a new dataframe from grouping part of the string into a column.
import pandas
df = pandas.DataFrame([{'A': 'string_300_bla1', 'B': "Hi", 'C': 3},
{'A': 'string_300_blaa2', 'B': "Hello", 'C': 4},
{'A': 'string_487_blaaa1', 'B': "nice", 'C': 9},
{'A': 'string_487_blaaa2', 'B': "day", 'C': 6}])
I want to make a groupby from this part of the string
string_300_bla1
I tried:
import re
dfs = df['A'].str.contains('.*_\d+_.*', re.IGNORECASE).groupby(df['B'])
My output:
<pandas.core.groupby.generic.SeriesGroupBy object at 0x00000279EFD009E8>
Good output:
dfs = pandas.DataFrame([{'A': 'string_300', 'B': "Hi\n\nHello"},
{'A': 'string_487', 'B': "nice\n\nday"}])