set_index()
- 函數(shù)原型:DataFrame.set_index(keys, drop=True, append=False, inplace=False, verify_integrity=False)
- 參數(shù)解釋:
keys:列標(biāo)簽或列標(biāo)簽/數(shù)組列表,需要設(shè)置為索引的列
drop:默認(rèn)為True,刪除用作新索引的列
append:默認(rèn)為False,是否將列附加到現(xiàn)有索引
inplace:默認(rèn)為False蜓洪,適當(dāng)修改DataFrame(不要?jiǎng)?chuàng)建新對(duì)象)
verify_integrity:默認(rèn)為false犀填,檢查新索引的副本噪奄。否則踊沸,請(qǐng)將檢查推遲到必要時(shí)進(jìn)行扔役。將其設(shè)置為false將提高該方法的性能肄鸽。
入門級(jí)api
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2019-06-06 13:09
# @Author : LiYahui
# @Description : set_index demo
import pandas as pd
data = {'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11'],
'B': ['B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11'],
'C': ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11'],
'D': ['D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11']}
df = pd.DataFrame(data)
# print(df)
'''
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
'''
# drop=True
df1 = df.set_index("A", drop=True, append=False, inplace=False, verify_integrity=False)
# print(df1)
'''
B C D
A
A0 B0 C0 D0
A1 B1 C1 D1
A2 B2 C2 D2
A3 B3 C3 D3
A4 B4 C4 D4
A5 B5 C5 D5
A6 B6 C6 D6
A7 B7 C7 D7
A8 B8 C8 D8
A9 B9 C9 D9
A10 B10 C10 D10
A11 B11 C11 D11
'''
# drop=False
df2 = df.set_index("A", drop=False, append=False, inplace=False, verify_integrity=False)
# print(df2)
'''
A B C D
A
A0 A0 B0 C0 D0
A1 A1 B1 C1 D1
A2 A2 B2 C2 D2
A3 A3 B3 C3 D3
A4 A4 B4 C4 D4
A5 A5 B5 C5 D5
A6 A6 B6 C6 D6
A7 A7 B7 C7 D7
A8 A8 B8 C8 D8
A9 A9 B9 C9 D9
A10 A10 B10 C10 D10
A11 A11 B11 C11 D11
'''
# append=True
df3 = df.set_index("A", drop=False, append=True, inplace=False, verify_integrity=False)
# print(df3)
'''
A B C D
A
0 A0 A0 B0 C0 D0
1 A1 A1 B1 C1 D1
2 A2 A2 B2 C2 D2
3 A3 A3 B3 C3 D3
4 A4 A4 B4 C4 D4
5 A5 A5 B5 C5 D5
6 A6 A6 B6 C6 D6
7 A7 A7 B7 C7 D7
8 A8 A8 B8 C8 D8
9 A9 A9 B9 C9 D9
10 A10 A10 B10 C10 D10
11 A11 A11 B11 C11 D11
'''
# inplance=True
df4 = df.set_index("A", drop=False, append=True, inplace=True, verify_integrity=False)
print(df4)
# 不知道為什么
'''
None
'''
reset_index()
- 函數(shù)原型:DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
- 參數(shù)解釋:
level:int卫病、str、tuple或list典徘,默認(rèn)無(wú)蟀苛,僅從索引中刪除給定級(jí)別。默認(rèn)情況下移除所有級(jí)別逮诲≈钠剑控制了具體要還原的那個(gè)等級(jí)的索引
drop:drop為False則索引列會(huì)被還原為普通列,否則會(huì)丟失
inplace:默認(rèn)為false梅鹦,適當(dāng)修改DataFrame(不要?jiǎng)?chuàng)建新對(duì)象)
col_level:int或str裆甩,默認(rèn)值為0,如果列有多個(gè)級(jí)別齐唆,則確定將標(biāo)簽插入到哪個(gè)級(jí)別嗤栓。默認(rèn)情況下,它將插入到第一級(jí)箍邮。
col_fill:對(duì)象茉帅,默認(rèn)‘’,如果列有多個(gè)級(jí)別锭弊,則確定其他級(jí)別的命名方式堪澎。如果沒(méi)有,則重復(fù)索引名 - 注:reset_index還原分為兩種類型味滞,第一種是對(duì)原DataFrame進(jìn)行reset全封,第二種是對(duì)使用過(guò)set_index()函數(shù)的DataFrame進(jìn)行reset
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2019-06-06 13:21
# @Author : LiYahui
# @Description : reset_index demo
import pandas as pd
data = {'A': ['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11'],
'B': ['B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'B10', 'B11'],
'C': ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11'],
'D': ['D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'D10', 'D11']}
df = pd.DataFrame(data)
# print(df)
'''
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
'''
# drop=True
df1 = df.set_index("A", drop=True, append=False, inplace=False, verify_integrity=False)
# print(df1)
'''
B C D
A
A0 B0 C0 D0
A1 B1 C1 D1
A2 B2 C2 D2
A3 B3 C3 D3
A4 B4 C4 D4
A5 B5 C5 D5
A6 B6 C6 D6
A7 B7 C7 D7
A8 B8 C8 D8
A9 B9 C9 D9
A10 B10 C10 D10
A11 B11 C11 D11
'''
# drop=False
df2 = df1.reset_index(drop=False)
# print(df2)
'''
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
'''
# drop=True
df3=df1.reset_index(drop=True)
# print(df3)
'''
B C D
0 B0 C0 D0
1 B1 C1 D1
2 B2 C2 D2
3 B3 C3 D3
4 B4 C4 D4
5 B5 C5 D5
6 B6 C6 D6
7 B7 C7 D7
8 B8 C8 D8
9 B9 C9 D9
10 B10 C10 D10
11 B11 C11 D11
'''
df4=df.reset_index(drop=False)
# print(df4)
'''
index A B C D
0 0 A0 B0 C0 D0
1 1 A1 B1 C1 D1
2 2 A2 B2 C2 D2
3 3 A3 B3 C3 D3
4 4 A4 B4 C4 D4
5 5 A5 B5 C5 D5
6 6 A6 B6 C6 D6
7 7 A7 B7 C7 D7
8 8 A8 B8 C8 D8
9 9 A9 B9 C9 D9
10 10 A10 B10 C10 D10
11 11 A11 B11 C11 D11
'''
df5=df.reset_index(drop=True)
print(df5)
'''
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
'''
添加多個(gè)字段的index
demo級(jí)別的代碼
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2019-06-06 13:28
# @Author : LiYahui
# @Description : reset_index_demo2
import pandas as pd
data = {'a': ['bar', 'bar', 'foo', 'foo'],
'b': ['one', 'two', 'one', 'two'],
'c': ['z', 'x', 'y', 'w'],
'd': [1.0, 2.0, 3.0, 4.0]}
df = pd.DataFrame(data)
# print(df)
'''
a b c d
0 bar one z 1.0
1 bar two x 2.0
2 foo one y 3.0
3 foo two w 4.0
'''
df1 = df.set_index(['a', 'b'])
# print(df1)
'''
c d
a b
bar one z 1.0
two x 2.0
foo one y 3.0
two w 4.0
'''
df2 = df1.reset_index()
# print(df2)
'''
a b c d
0 bar one z 1.0
1 bar two x 2.0
2 foo one y 3.0
3 foo two w 4.0
'''
df3 = df1.reset_index(['a', 'b'])
print(df3)
'''
a b c d
0 bar one z 1.0
1 bar two x 2.0
2 foo one y 3.0
3 foo two w 4.0
'''
df4 = df1.reset_index('a')
# print(df4)
'''
a c d
b
one bar z 1.0
two bar x 2.0
one foo y 3.0
two foo w 4.0
'''
df5=df1.reset_index('b')
print(df5)
'''
b c d
a
bar one z 1.0
bar two x 2.0
foo one y 3.0
foo two w 4.0
'''