Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select multiple columns without deprecated ix in pandas

Tags:

python

pandas

In python, for data slicing in DataFrame in package pandas, .ix is already deprecated from pandas 0.20.0. The official website offers alternative solutions with either .loc or .iloc to do the hybrid selection (http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html). The .index could help extract multiple rows. By contrast, the columns.get_loc seems can only select one column at most. Is there an alternative function available that can be used to extract multiple columns in a hybrid manner using .iloc?

like image 344
johnsonzhj Avatar asked Oct 16 '25 07:10

johnsonzhj


1 Answers

Yes, function is called Index.get_indexer and return position of columns or index by list of names.

Use it this way:

df = pd.DataFrame({
         'a':[4,5,4,5,5,4],
         'b':[7,8,9,4,2,3],
         'c':[1,3,5,7,1,0],
         'd':[5,3,6,9,2,4],
}, index=list('ABCDEF'))
print (df)
   a  b  c  d
A  4  7  1  5
B  5  8  3  3
C  4  9  5  6
D  5  4  7  9
E  5  2  1  2
F  4  3  0  4

cols = ['a','b','c']
df1 = df.iloc[1, df.columns.get_indexer(cols)]
print (df1)
a    5
b    8
c    3
Name: B, dtype: int64

df11 = df.iloc[[1], df.columns.get_indexer(cols)]
print (df11)
   a  b  c
B  5  8  3

idx = ['A','C']
df2 = df.iloc[df.index.get_indexer(idx), 2:]
print (df2)
   c  d
A  1  5
C  5  6
like image 176
jezrael Avatar answered Oct 17 '25 20:10

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!