Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select an element in array column of a data frame?

I have the following data frame:

pa=pd.DataFrame({'a':np.array([[1.,4.],[2.],[3.,4.,5.]])})

I want to select the column 'a' and then only a particular element (i.e. first: 1., 2., 3.)

What do I need to add to:

pa.loc[:,['a']]

?

like image 756
jankos Avatar asked Sep 26 '14 22:09

jankos


1 Answers

pa.loc[row] selects the row with label row.

pa.loc[row, col] selects the cells which are the instersection of row and col

pa.loc[:, col] selects all rows and the column named col. Note that although this works it is not the idiomatic way to refer to a column of a dataframe. For that you should use pa['a']

Now you have lists in the cells of your column so you can use the vectorized string methods to access the elements of those lists like so.

pa['a'].str[0] #first value in lists
pa['a'].str[-1] #last value in lists
like image 72
b10n Avatar answered Nov 03 '22 02:11

b10n