Skip to content

Pandas things

Marinna Martini edited this page Sep 22, 2019 · 3 revisions

This is just a munch of miscellaneous tricks that keep slipping out of my mind. One good way to think about a Pandas DataFrame is that it act very much like a spreadsheet.

referencing items in the DataFrame by row count

iloc for numerical row reference

Using a numerical index to set or get a value in a row in a DataFrame:
dataframe.iloc[row_number, dataframe.columns.get_loc('the_column_name')] = new_value
or
value = dataframe.iloc[row_number, dataframe.columns.get_loc('the_column_name')]
unpacking it: dataframe.columns.get_loc('the_column_name') returns the column count for the location of the column the_column_name, and the iloc simply wants the row, column location to look up. Note that there is no analogous methodology 'dataframe.rows.get_loc'! You have to use an index.

referencing items in the DataFrame using an index

The brillian thing about Pandas is that an index can be - anything!

by index

dataframe.index[0] will return the value of the index of the first row, if this is a time series it will be a Timestamp object