2

I have a data frame like this:

>df = pd.DataFrame({'A':['M',2,3],'B':['M',2,3],'AA':['N',20,30],'BB':['N',20,30]})
>df = df.rename(columns={df.columns[2]: 'A'})
>df = df.rename(columns={df.columns[3]: 'B'})
>df

  A  B  A  B
0 M  M  N  N
1 2  2  20 20
2 3  3  30 30

and I have to split the data frame vertically by row index 0 = 'M' and 'N':

  A  B
0 M  M
1 2  2
2 3  3

  A  B
0 N  N
1 20 20
2 30 30

The data in the data frame comes from an Excel sheet and the column names are not unique. Thanks for help!

2
  • you need to split data into 2 separate data frames? Commented Jun 18, 2019 at 9:51
  • Have you tried iloc? Commented Jun 18, 2019 at 9:53

3 Answers 3

2

This should get the job done:

df.loc[:,df.iloc[0, :] == "M"]
df.loc[:,df.iloc[0, :] == "N"]
Sign up to request clarification or add additional context in comments.

Comments

1

Use pandas iloc for selecting columns:

=^..^=

import pandas as pd

df = pd.DataFrame({'A':['M',2,3],'B':['M',2,3],'AA':['N',20,30],'BB':['N',20,30]})
df = df.rename(columns={df.columns[2]: 'A'})
df = df.rename(columns={df.columns[3]: 'B'})

df1 = df.iloc[:, :2]
df2 = df.iloc[:, 2:]

Output:

   A  B
0  M  M
1  2  2
2  3  3
    A   B
0   N   N
1  20  20
2  30  30

Comments

1

Use list comprehension with loc as:

dfs = [df.loc[:, df.loc[0,:].eq(s)] for s in ['M','N']]

This gives seperate dataframes in list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.