import numpy as np
import pandas as pd
df = pd.read_csv(r"C:\SAIC\DataHunter\GitHub\joyful-pandas\data\table.csv",index_col='ID')
df.head()
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1101 | S_1 | C_1 | M | street_1 | 173 | 63 | 34.0 | A+ |
---|
1102 | S_1 | C_1 | F | street_2 | 192 | 73 | 32.5 | B+ |
---|
1103 | S_1 | C_1 | M | street_2 | 186 | 82 | 87.2 | B+ |
---|
1104 | S_1 | C_1 | F | street_2 | 167 | 81 | 80.4 | B- |
---|
1105 | S_1 | C_1 | F | street_4 | 159 | 64 | 84.8 | B+ |
---|
df.loc[1103]
School S_1
Class C_1
Gender M
Address street_2
Height 186
Weight 82
Math 87.2
Physics B+
Name: 1103, dtype: object
df.loc[[1102,2304]]
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1102 | S_1 | C_1 | F | street_2 | 192 | 73 | 32.5 | B+ |
---|
2304 | S_2 | C_3 | F | street_6 | 164 | 81 | 95.5 | A- |
---|
df.loc[2402::-1].head()
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
2402 | S_2 | C_4 | M | street_7 | 166 | 82 | 48.7 | B |
---|
2401 | S_2 | C_4 | F | street_2 | 192 | 62 | 45.3 | A |
---|
2305 | S_2 | C_3 | M | street_4 | 187 | 73 | 48.9 | B |
---|
2304 | S_2 | C_3 | F | street_6 | 164 | 81 | 95.5 | A- |
---|
2303 | S_2 | C_3 | F | street_7 | 190 | 99 | 65.9 | C |
---|
df.loc[:,'Height'].head()
ID
1101 173
1102 192
1103 186
1104 167
1105 159
Name: Height, dtype: int64
df.loc[:,'Height':'Math'].head()
| Height | Weight | Math |
---|
ID | | | |
---|
1101 | 173 | 63 | 34.0 |
---|
1102 | 192 | 73 | 32.5 |
---|
1103 | 186 | 82 | 87.2 |
---|
1104 | 167 | 81 | 80.4 |
---|
1105 | 159 | 64 | 84.8 |
---|
df.loc[1102:2401:3,'Height':'Math'].head()
| Height | Weight | Math |
---|
ID | | | |
---|
1102 | 192 | 73 | 32.5 |
---|
1105 | 159 | 64 | 84.8 |
---|
1203 | 160 | 53 | 58.8 |
---|
1301 | 161 | 68 | 31.5 |
---|
1304 | 195 | 70 | 85.2 |
---|
df.loc[lambda x:x['Gender']=='M'].head()
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1101 | S_1 | C_1 | M | street_1 | 173 | 63 | 34.0 | A+ |
---|
1103 | S_1 | C_1 | M | street_2 | 186 | 82 | 87.2 | B+ |
---|
1201 | S_1 | C_2 | M | street_5 | 188 | 68 | 97.0 | A- |
---|
1203 | S_1 | C_2 | M | street_6 | 160 | 53 | 58.8 | A+ |
---|
1301 | S_1 | C_3 | M | street_4 | 161 | 68 | 31.5 | B+ |
---|
def f(x):
return [1101,1103]
df.loc[f]
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1101 | S_1 | C_1 | M | street_1 | 173 | 63 | 34.0 | A+ |
---|
1103 | S_1 | C_1 | M | street_2 | 186 | 82 | 87.2 | B+ |
---|
df.loc[df['Address'].isin(['street_7','street_4'])].head()
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1105 | S_1 | C_1 | F | street_4 | 159 | 64 | 84.8 | B+ |
---|
1202 | S_1 | C_2 | F | street_4 | 176 | 94 | 63.5 | B- |
---|
1301 | S_1 | C_3 | M | street_4 | 161 | 68 | 31.5 | B+ |
---|
1303 | S_1 | C_3 | M | street_7 | 188 | 82 | 49.7 | B |
---|
2101 | S_2 | C_1 | M | street_7 | 174 | 84 | 83.3 | C |
---|
df.loc[[True if i[-1]=='4' or i[-1]=='7' else False for i in df['Address'].values]].head()
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1105 | S_1 | C_1 | F | street_4 | 159 | 64 | 84.8 | B+ |
---|
1202 | S_1 | C_2 | F | street_4 | 176 | 94 | 63.5 | B- |
---|
1301 | S_1 | C_3 | M | street_4 | 161 | 68 | 31.5 | B+ |
---|
1303 | S_1 | C_3 | M | street_7 | 188 | 82 | 49.7 | B |
---|
2101 | S_2 | C_1 | M | street_7 | 174 | 84 | 83.3 | C |
---|
df.iloc[3]
School S_1
Class C_1
Gender F
Address street_2
Height 167
Weight 81
Math 80.4
Physics B-
Name: 1104, dtype: object
df.iloc[3:5]
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1104 | S_1 | C_1 | F | street_2 | 167 | 81 | 80.4 | B- |
---|
1105 | S_1 | C_1 | F | street_4 | 159 | 64 | 84.8 | B+ |
---|
df.iloc[:,3].head()
ID
1101 street_1
1102 street_2
1103 street_2
1104 street_2
1105 street_4
Name: Address, dtype: object
df.iloc[:,7::-2].head()
| Physics | Weight | Address | Class |
---|
ID | | | | |
---|
1101 | A+ | 63 | street_1 | C_1 |
---|
1102 | B+ | 73 | street_2 | C_1 |
---|
1103 | B+ | 82 | street_2 | C_1 |
---|
1104 | B- | 81 | street_2 | C_1 |
---|
1105 | B+ | 64 | street_4 | C_1 |
---|
df.iloc[3::4,7::-2].head()
| Physics | Weight | Address | Class |
---|
ID | | | | |
---|
1104 | B- | 81 | street_2 | C_1 |
---|
1203 | A+ | 53 | street_6 | C_2 |
---|
1302 | A- | 57 | street_1 | C_3 |
---|
2101 | C | 84 | street_7 | C_1 |
---|
2105 | A | 81 | street_4 | C_1 |
---|
df.iloc[lambda x:[3]].head()
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1104 | S_1 | C_1 | F | street_2 | 167 | 81 | 80.4 | B- |
---|
s = pd.Series(df['Math'],index=df.index)
s[1101]
34.0
s[lambda x: x.index[16::-6]]
ID
2102 50.6
1301 31.5
1105 84.8
Name: Math, dtype: float64
s[lambda x: [16::-6]]
File "<ipython-input-21-ada17acead14>", line 1
s[lambda x: [16::-6]]
^
SyntaxError: invalid syntax
s[0:4]
ID
1101 34.0
1102 32.5
1103 87.2
1104 80.4
Name: Math, dtype: float64
s[s>80]
ID
1103 87.2
1104 80.4
1105 84.8
1201 97.0
1302 87.7
1304 85.2
2101 83.3
2205 85.4
2304 95.5
Name: Math, dtype: float64
df.loc[df['Math']>60,(df[:8]['Address']=='street_6').values].head()
| Physics |
---|
ID | |
---|
1103 | B+ |
---|
1104 | B- |
---|
1105 | B+ |
---|
1201 | A- |
---|
1202 | B- |
---|
df[:8]
| School | Class | Gender | Address | Height | Weight | Math | Physics |
---|
ID | | | | | | | | |
---|
1101 | S_1 | C_1 | M | street_1 | 173 | 63 | 34.0 | A+ |
---|
1102 | S_1 | C_1 | F | street_2 | 192 | 73 | 32.5 | B+ |
---|
1103 | S_1 | C_1 | M | street_2 | 186 | 82 | 87.2 | B+ |
---|
1104 | S_1 | C_1 | F | street_2 | 167 | 81 | 80.4 | B- |
---|
1105 | S_1 | C_1 | F | street_4 | 159 | 64 | 84.8 | B+ |
---|
1201 | S_1 | C_2 | M | street_5 | 188 | 68 | 97.0 | A- |
---|
1202 | S_1 | C_2 | F | street_4 | 176 | 94 | 63.5 | B- |
---|
1203 | S_1 | C_2 | M | street_6 | 160 | 53 | 58.8 | A+ |
---|
可用的时间 还是太短了,争取少加班吧