3 months ago

The behavior of passing an integer, string, slicing, list of integers, list of strings to a DataFrame:

df[0] # error

df['col_0'] # A Series of col_0


# The following behaviors are downright confusing

df[0:1] # A DataFrame of row 0

df[[0]] # A DataFrame of col 0

df[['col_0']] # A DataFrame of col 0

Summary

  • If a slicing or a list is passed, a DataFrame object is returned

    • If a slicing is passed, a DataFrame of rows are returned
    • If a list is passed, a DataFrame of cols are turned
  • If a String is passed, a Series of a column is returned

← Pandas loc vs iloc
 
comments powered by Disqus