-
Notifications
You must be signed in to change notification settings - Fork 1
Pandas things
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.
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.
The brillian thing about Pandas is that an index can be - anything!
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