#一:索引對象
#Series的索引和DataFrame的行和列索引都是索引對象;
#索引對象不可以進行修改
In [23]:
obj2.index
Out[23]:
Index(['a', 'b', 'c', 'd'], dtype='object')
In [24]:
df.index
Out[24]:
RangeIndex(start=0, stop=4, step=1, name='id')
In [25]:
df.columns
Out[25]:
Index(['name', 'sex', 'year', 'city'], dtype='object', name='std_info')
In [26]:
'sex' in df.columns
Out[26]:
True
In [27]:
'f' in df.columns
Out[27]:Falsef
#二:基本操作爲:“增,刪,改,查”
#1.增加 append函數傳入字典結構數據
new_dara = {'city':'武漢','name':'小李','gex':'male','year':2002 }
df = df.append(new_dara,ignore_index = True)
df
name |
sex |
year |
city |
gex |
|
---|---|---|---|---|---|
0 |
張三 |
female |
2001 |
北京 |
NaN |
1 |
李四 |
female |
2002 |
上海 |
NaN |
2 |
王五 |
male |
2003 |
廣州 |
NaN |
3 |
小明 |
male |
2002 |
北京 |
NaN |
4 |
小李 |
NaN |
2002 |
武漢 |
|
#新建一列用於存放信息; df['class'] = 2018 df#新建一列用於存放信息;
df['class'] = 2018
df
Out[30]:
name | sex | year | city | gex | class | |
---|---|---|---|---|---|---|
0 | 張三 | female | 2001 | 北京 | NaN | 2018 |
1 | 李四 | female | 2002 | 上海 | NaN | 2018 |
2 | 王五 | male | 2003 | 廣州 | NaN | 2018 |
3 | 小明 | male | 2002 | 北京 | NaN | 2018 |
4 | 小李 | NaN | 2002 | 武漢 | male |
2018 |
#新增的列中的數值不一樣,可以傳入列表或數組結構數據進行賦值
df['math']=[92,78,69,82,66]
df
Out[32]:
name | sex | year | city | gex | class | math | |
---|---|---|---|---|---|---|---|
0 | 張三 | female | 2001 | 北京 | NaN | 2018 | 92 |
1 | 李四 | female | 2002 | 上海 | NaN | 2018 | 78 |
2 | 王五 | male | 2003 | 廣州 | NaN | 2018 | 69 |
3 | 小明 | male | 2002 | 北京 | NaN | 2018 | 82 |
4 | 小李 | NaN | 2002 | 武漢 | male | 2018 |
66 |
#2.刪除
通過drop函數刪除指定軸上的信息
new_df = df.drop(2)
new_df
Out[33]:
name | sex | year | city | gex | class | math | |
---|---|---|---|---|---|---|---|
0 | 張三 | female | 2001 | 北京 | NaN | 2018 | 92 |
1 | 李四 | female | 2002 | 上海 | NaN | 2018 | 78 |
3 | 小明 | male | 2002 | 北京 | NaN | 2018 | 82 |
4 | 小李 | NaN | 2002 | 武漢 | male | 2018 | 66 |
In [34]:
new_df = new_df.drop('class',axis=1)#刪除列
new_df
Out[34]:
name | sex | year | city | gex | math | |
---|---|---|---|---|---|---|
0 | 張三 | female | 2001 | 北京 | NaN | 92 |
1 | 李四 | female | 2002 | 上海 | NaN | 78 |
3 | 小明 | male | 2002 | 北京 | NaN | 82 |
4 | 小李 | NaN | 2002 | 武漢 | male | 66 |
#3.修改 '改'指的是行和列索引標籤的修改,通過rename函數
#inplace可在原數據上修改
new_df.rename(index={3:2,4:3},columns={'math':'Math'},inplace=True)
new_df
name | sex | year | city | gex | Math | |
---|---|---|---|---|---|---|
0 | 張三 | female | 2001 | 北京 | NaN | 92 |
1 | 李四 | female | 2002 | 上海 | NaN | 78 |
2 | 小明 | male | 2002 | 北京 | NaN | 82 |
2 | 小李 | NaN | 2002 | 武漢 | male | 66 |
#三:索引選取
#1.選取列
>>通過中括號[]
#2.選取行(索引位置0到N-1)
方法一:切片方法;eg:df2['李四','王五']
方法二:通過loc和iloc方法實現;
loc:按照行索引標籤選取數據;eg:df2.loc[['張三','王五']]
iloc:按照行索引位置選取數據;eg:df2.iloc[[1,3]]
#3.選取行和列
通過ix()方法支持索引標籤和索引位置來進行數據的選取;
eg:df2.ix[['張三','王五'],0:2] #獲取行
eg:df2.ix[:,['張三','王五']] #獲取列
#通過set_index方法和reset_index方法設置行索引;