User Tools

Site Tools


get_first_element_of_series_if_not_empty

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
get_first_element_of_series_if_not_empty [2021/01/16 07:32] – ↷ Page name changed from get_first_element_of_a_series_only_if_it_is_not_empty to get_first_element_of_series_if_not_empty rajuget_first_element_of_series_if_not_empty [2021/01/17 02:04] (current) – [Solution] raju
Line 1: Line 1:
 ===== get first element of series if not empty ===== ===== get first element of series if not empty =====
  
-tags | apply iloc[0] on a series only if it is not empty, pandas check for emptiness before applying iloc, check for emptiness before getting the first element+tags | apply iloc[0] on a series only if it is not empty, pandas check for emptiness before applying iloc, check for emptiness before getting the first element, pandas iloc on empty series, pandas series return first value if not empty
  
 ==== Task ==== ==== Task ====
Line 12: Line 12:
 df.loc[mask]['C'].iloc[0] df.loc[mask]['C'].iloc[0]
 </code> </code>
-since iloc[0] will throw the exception+since iloc[0] will throw
 <code> <code>
 IndexError: single positional indexer is out-of-bounds IndexError: single positional indexer is out-of-bounds
 </code> </code>
-if df.loc[mask] is empty.+exception if df.loc[mask] is empty.
  
 For example For example
Line 58: Line 58:
 next(iter(df.loc[mask]['C']), None) next(iter(df.loc[mask]['C']), None)
 </code> </code>
 +or next(iter(series), default) in general.
 +
 With the above example With the above example
 <code> <code>
get_first_element_of_series_if_not_empty.1610782354.txt.gz · Last modified: 2021/01/16 07:32 by raju