Skip to content Skip to sidebar Skip to footer

Keep/slice Specific Columns In Pandas

I know about these column slice methods: df2 = df[['col1', 'col2', 'col3']] and df2 = df.ix[:,0:2] but I'm wondering if there is a way to slice columns from the front/middle/end of

Solution 1:

IIUC, the simplest way I can think of would be something like this:

>>>import pandas as pd>>>import numpy as np>>>df = pd.DataFrame(np.random.randn(5, 10))>>>df[list(df.columns[:2]) + [7]]
          0         1         7
0  0.210139  0.533249  1.780426
1  0.382136  0.083999 -0.392809
2 -0.237868  0.493646 -1.208330
3  1.242077 -0.781558  2.369851
4  1.910740 -0.643370  0.982876

where the list call isn't optional because otherwise the Index object will try to vector-add itself to the 7.

It would be possible to special-case something like numpy's r_ so that

df[col_[:2, "col5", 3:6]]

would work, although I don't know if it would be worth the trouble.

Solution 2:

If your column names have information that you can filter for, you could use df.filter(regex='name*'). I am using this to filter between my 189 data channels from a1_01 to b3_21 and it works fine.

Solution 3:

Not sure exactly what you're asking. If you want the first and last 5 rows of a specific column, you can do something like this

df = pd.DataFrame({'col1': np.random.randint(0,3,1000),
               'col2': np.random.rand(1000),
               'col5': np.random.rand(1000)}) 
In [36]: df['col5']
Out[36]: 
0     0.566218
1     0.305987
2     0.852257
3     0.932764
4     0.185677
...
996    0.268700
997    0.036250
998    0.470009
999    0.361089
Name: col5, Length: 1000 
In [38]: df['col5'][(df.index < 5) | (df.index > (len(df) - 5))]
Out[38]: 
0      0.566218
1      0.305987
2      0.852257
3      0.932764
4      0.185677
996    0.268700
997    0.036250
998    0.470009
999    0.361089
Name: col5

Or, more generally, you could write a function

In [41]: def head_and_tail(df, n=5):
    ...:     returndf[(df.index < n) | (df.index > (len(df) - n))] 
In [44]: head_and_tail(df, 7)
Out[44]: 
     col1      col2      col5
0       0  0.489944  0.566218
1       1  0.639213  0.305987
2       1  0.000690  0.852257
3       2  0.620568  0.932764
4       0  0.310816  0.185677
5       0  0.930496  0.678504
6       2  0.165250  0.440811
994     2  0.842181  0.636472
995     0  0.899453  0.830839
996     0  0.418264  0.268700
997     0  0.228304  0.036250
998     2  0.031277  0.470009
999     1  0.542502  0.361089 

Post a Comment for "Keep/slice Specific Columns In Pandas"