metaditch
metaditch

Reputation: 63

Grouping dataframe based on column similarities in Python

I have a dataframe with commonalities in groups of column names:

Sample1.Feature1 | Sample1.Feature2 | ... | Sample99.Feature1 | Sample99.Feature 2

And I'd like to reorder this as

|Sample1 ......................... | Sample99

|Feature 1, Feature 2 | ..... | Feature 1, Feature 2 |

I'd then have summary stats, e.g. mean, for Feature1, Feature2, grouped by Sample#. I've played with df.groupby() with no luck so far.

I hope my lack of table formatting skills doesn't distract from the question.

Thanks in advance.

Upvotes: 1

Views: 315

Answers (1)

piRSquared
piRSquared

Reputation: 294198

consider the dataframe df

df = pd.DataFrame(
    np.ones((1, 6)),
    columns='s1.f1 s1.f2 s1.f3 s2.f1 s2.f2 s2.f3'.split())
df

enter image description here


  • split the columns

df.columns = df.columns.str.split('.', expand=True)
df

enter image description here

Upvotes: 2

Related Questions