eduardo2111
eduardo2111

Reputation: 379

Pandas Dataframe split starting on a specific column

I have the following Pandas dataframe:

       a         b      c         d  Unnamed: 5 
1  43.91 -0.041619  43.91 -0.041619       43.91
2  43.39  0.011913  43.91 -0.041619       43.91
3  45.56 -0.048801  43.91 -0.041619       43.91
4  45.43  0.002857  43.91 -0.041619       43.91
5  45.33  0.002204  43.91 -0.041619       43.91
6  45.68 -0.007692  43.91 -0.041619       43.91
7  46.37 -0.014992  43.91 -0.041619       43.91
8  48.04 -0.035381  43.91 -0.041619       43.91
9  48.38 -0.007053  43.91 -0.04161        43.91

And I would like to do divide this dataframe, by calling the name column d by name, and get the following output:

df1 =

      a         b      c 
1  43.91 -0.041619  43.91
2  43.39  0.011913  43.91
3  45.56 -0.048801  43.91
4  45.43  0.002857  43.91
5  45.33  0.002204  43.91
6  45.68 -0.007692  43.91
7  46.37 -0.014992  43.91
8  48.04 -0.035381  43.91
9  48.38 -0.007053  43.91

df2 =

          d  Unnamed: 5 
1  -0.041619       43.91
2  -0.041619       43.91
3  -0.041619       43.91
4  -0.041619       43.91
5  -0.041619       43.91
6  -0.041619       43.91
7  -0.041619       43.91
8  -0.041619       43.91
9  -0.04161        43.91

Is it possible to achieve this output using pandas library?

Upvotes: 1

Views: 62

Answers (1)

jezrael
jezrael

Reputation: 863226

If need one DataFrame with excluded d column is used Index.get_loc for positions and selecting by DataFrame.iloc, for included is used seelcting by DataFrame.loc:

col = 'd'
df1 = df.iloc[:, :df.columns.get_loc(col)]
df2 = df.loc[:, col:]
print (df1)
       a         b      c
1  43.91 -0.041619  43.91
2  43.39  0.011913  43.91
3  45.56 -0.048801  43.91
4  45.43  0.002857  43.91
5  45.33  0.002204  43.91
6  45.68 -0.007692  43.91
7  46.37 -0.014992  43.91
8  48.04 -0.035381  43.91
9  48.38 -0.007053  43.91

print (df2)
          d  Unnamed: 5
1 -0.041619       43.91
2 -0.041619       43.91
3 -0.041619       43.91
4 -0.041619       43.91
5 -0.041619       43.91
6 -0.041619       43.91
7 -0.041619       43.91
8 -0.041619       43.91
9 -0.041610       43.91

Upvotes: 2

Related Questions