In [1]:
import pandas as pd
In [2]:
pd.__version__
Out[2]:
In [157]:
df_default_index = pd.DataFrame({
'name':['john','mary','peter','nancy','gary'],
'age':[22,33,27,22,31],
'state':['AK','DC','CA','CA','NY']
})
df_default_index
Out[157]:
In [158]:
df_default_index.loc[[0]]
Out[158]:
In [159]:
df_default_index.loc[[2,3]]
Out[159]:
In [130]:
df_name_index = pd.DataFrame(
index=['john','mary','peter','nancy','gary'],
data={
'age':[22,33,27,22,31],
'state':['AK','DC','CA','CA','NY']
}
)
In [131]:
df_name_index
Out[131]:
In [133]:
df_name_index.loc[['peter']]
Out[133]:
In [143]:
df_name_index.loc[['john']]
Out[143]:
In [134]:
df_name_index.iloc[[0]]
Out[134]:
In [141]:
df_name_index.iloc[[2,3,4]]
Out[141]:
In [135]:
df_default_index.iloc[[0]]
Out[135]:
In [142]:
df_default_index.iloc[[2,3,4]]
Out[142]:
In [155]:
df_set = pd.DataFrame({
'name':['john','mary','peter','nancy','gary'],
'age':[22,33,27,22,31],
'state':['AK','DC','CA','CA','NY']
})
df_set
Out[155]:
In [156]:
df_set.loc[0,'name'] = 'bartholomew'
df_set.loc[3, 'age'] = 39
df_set
Out[156]:
In [148]:
df_name_index.loc['john','age'] = 99
df_name_index
Out[148]:
In [53]:
df_over_30_years = df[df['age']>30]
df_over_30_years
Out[53]:
In [54]:
df_over_30_years['new_column'] = 'some_value'
In [49]:
df_over_30_years = df.copy()[df['age']>30]
df_over_30_years
Out[49]:
In [50]:
df_over_30_years['new_column'] = 'some_value'
In [51]:
df_over_30_years
Out[51]: