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']]
?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With