User Tools

Site Tools


programming:python:pandas

pandas dataframe

command examples explanation / remarks
.loc[] df.loc[row_name_str, col_name_str]
df.loc[row_names, col_names]
d.loc[“july”, “apples”]=[5700]
d.loc{{“january”,“february”,“march”}, “apples”}=[0,0,0] !correct example!
get or set value(s) in dataframe sub-range. Single value should be inside brackets []
[~link~]link
`iloc[]` `df.iloc[row_i, col_i]`<pp>`new_df=df.iloc[row_i1:row_i2, col_i]`<p>`new_df=df.ilocrow_i1,row_i2,row_i3], [col_i1,col_i2` get or set values in dataframe sub-range. returns a dataframe.
.columns col_titles_list = df.columns get list of the column titles (headers)
`columns.get_loc()` `col_i=df.columns.get_loc(col_name_s)` get index of column by name (string)
`at[]` `df.at[row_s, col_s]` get or set value in a single cell, by row and column names
`iat[]` `df.iat[row_i, col_i]` get or set value in a single cell, by row and column integer index
drop() new_df = df.drop('row_title_to_drop')<pp>new_df = df.drop(list_of_row_titles_to_drop)<p>new_df = df.drop(index=2)<pp>new_df = df.drop(index=[3,4,6]) function that returns a dataframe in which the row(s) have been removed.
equals() same_data_b = df1.equals(df2) function that returns a boolean, True if dataframes have the same information, otherwise returns False
columns.get_loc() (example?) get integer location of column, by its name
index.get_loc() (example?) get integer location of row, by its name
.sort_values(column_name) output_df = df.sort_values(col_name)
output_df = df.sort_values(by=[col_name_1, col_name_2, …], ascending=True)
sort DataFrame by given column(s)
NOTE: row-titles also move, so use iloc instead of loc

import pandas # pip install pandas

programming/python/pandas.txt · Last modified: 2023/07/20 07:32 by levor