What is the difference between iloc, loc, ix for Pandas DataFrame?

  • loc gets rows (or columns) with particular labels from the index. 


# delete columns which ends with column name "Name"
df1 = df.loc[:, ~df.columns.str.endswith('Name')]
# delete columns which contain "Name" in column name
df1 = df.loc[:, ~df.columns.str.contains('Name')]
  • iloc gets rows (or columns) at particular positions in the index (so it only takes integers).

  • ix usually tries to behave like loc but falls back to behaving like iloc if a label is not present in the index.

No comments:

Post a Comment