Sunday, 15 May 2011

python - Remove empty lists in pandas series -


I have a long series like the following:

  chain = pd.Series ( [[152]: series [151]: 0 [(1, 2]] 1 [(3, 5)] 2 [] 3 [(3, 5)] dtype: object  

I want to remove all the entries with empty entries

  series == [[(1,2)]] = = [(1,2)] value error: The different lengths were: 4 vs 1  

This is very strange, because in the general example below, indexing works like the above:

 In  [146] ]: Pd .eries ([1,2,3]) == [3] outside [146]: 0 false 1 false 2 true dtype: bool  

P.S. I would like to divide the tuple into two column dataframes.

You can see that the lists Whether or not by using str.len () :

  series.str.len () == 0  

and then Use this boolean series to remove rows with empty lists.

If each of your listings is a two-tuple (or other empty) list, then you can use a two-column DataFrame twice (using a str accessor twice) To choose the first element of, then to reach the elements of the tuple):

  pd.DataFrame ({'A': series.str [0] .str [0], 'b ': Series.str [0] .str [1]})  

Unavailable entries by default NaN by this method.


No comments:

Post a Comment