loc dataframe code example

Example 1: isolate row based on index pandas

dfObj.iloc[: , [0, 2]]

Example 2: pd.loc

>>> df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
...      index=['cobra', 'viper', 'sidewinder'],
...      columns=['max_speed', 'shield'])
>>> df
            max_speed  shield
cobra               1       2
viper               4       5
sidewinder          7       8

>>> df.loc[['viper', 'sidewinder']]
            max_speed  shield
viper               4       5
sidewinder          7       8

Example 3: pandas loc for list

df.loc[df['channel'].isin(['sale','fullprice'])]

Example 4: dataframe loc change values

%timeit df.at['Bill', 'language'] = 'Python'

Example 5: df.loc a list of index

df[df.index.isin([1,3])]