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

后端 未结 2 1328
囚心锁ツ
囚心锁ツ 2020-12-06 19:07

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 on

2条回答
  •  一向
    一向 (楼主)
    2020-12-06 19:36

    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
    

提交回复
热议问题