Quantcast
Channel: Cheng's Blog
Viewing all articles
Browse latest Browse all 10

Pandas selecting rows and columns

$
0
0

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


Viewing all articles
Browse latest Browse all 10

Trending Articles