python - How to split a pandas dataframe based on column prefix -
i have dataframe imported csv. goes this:
df a.1 b.1 a.2 b.2 1 1 1 1 2 2 2 2
my question is, efficient way turn seperate data frames comprised of a's , b's
df_a a.1 a.2 1 1 2 2 df_b b.1 b.2 1 1 2 2
i not picky far column names, fine having them stripped 1 , 2 etc haven't been able find way this. open other/better ways accomplish trying in case doesn't make sense more knowledgable. thanks!
you use df.filter
regex patterns:
df_a, df_b = df.filter(regex=r'^a'), df.filter(regex=r'^b')
or
df_a, df_b = df.filter(like='a'), df.filter(like='b')
note if use like='a'
columns name contains 'a'
selected. if use regex=r'^a'
columns name begins a
selected.
in [7]: df out[7]: a.1 b.1 a.2 b.2 0 1 1 1 1 1 2 2 2 2 in [8]: df_a, df_b = df.filter(regex=r'^a'), df.filter(regex=r'^b') in [9]: df_a out[9]: a.1 a.2 0 1 1 1 2 2 in [10]: df_b out[10]: b.1 b.2 0 1 1 1 2 2
Comments
Post a Comment