maad.util.get_df_single_row
- maad.util.get_df_single_row(df, index, mode='iloc')[source]
Extract a single row from a dataframe keeping the DataFrame type (instead of becoming a Series).
- Parameters:
- dfpandas DataFrame
DataFrame with one or more rows
- indexinterger (in case of ‘iloc’) or index (in case of loc)
index could be the row number or the row index depending on the mode
- modestring, optional, default is ‘iloc’
choose between row number (‘iloc’) or row index (‘loc’) (see Pandas documentation for the difference)
- Returns:
- df_outpandas DataFrame
Single row DataFrame
Examples
>>> import pandas as pd >>> from maad.util import get_df_single_row >>> species = [ ('bird', 8, 'Yes' ,'NYC') , ('insect', 4, 'No','NYC' ) , ('mammal', 2, 'No','NYC' ) , ('frog', 3, 'Yes',"LA" ) ] >>> df = pd.DataFrame(species, columns = ['category', 'abundance', 'presence', 'location']) >>> print(df) category abundance presence location 0 bird 8 Yes NYC 1 insect 4 No NYC 2 mammal 2 No NYC 3 frog 3 Yes LA >>> df.loc[0] category bird abundance 8 presence Yes location NYC Name: 0, dtype: object >>> maad.util.get_df_single_row (df, index=0, mode='iloc') category abundance presence location 0 bird 8 Yes NYC
Now with index
>>> df_modified=df.set_index("category") >>> print(df_modified) abundance presence location category bird 8 Yes NYC insect 4 No NYC mammal 2 No NYC frog 3 Yes LA >>> df_modified.loc['insect'] abundance 4 presence No location NYC Name: insect, dtype: object >>> maad.util.get_df_single_row (df_modified, index='insect', mode='loc') abundance presence location insect 4 No NYC >>> maad.util.get_df_single_row (df_modified, index=1, mode='iloc') abundance presence location insect 4 No NYC