Pandas——1 第2章 索引

本文通过对UFO目击记录及Pokemon数据集的分析,探讨了不同条件下的数据特征与规律,包括UFO形状分布、地理位置热区及Pokemon的属性比例、种族值分析等。

1.7.2 2. 练习
1.7.2.1 【练习一】 现有一份关于UFO的数据集,请解决下列问题:
pd.read_csv(‘data/UFO.csv’).head()
datetime shape duration (seconds) latitude longitude
0 10/10/1949 20:30 cylinder 2700.0 29.883056 -97.941111
1 10/10/1949 21:00 light 7200.0 29.384210 -98.581082
2 10/10/1955 17:00 circle 20.0 53.200000 -2.916667
3 10/10/1956 21:00 circle 20.0 28.978333 -96.645833
4 10/10/1960 20:00 light 900.0 21.418056 -157.803611
1.7.2.2 (a)在所有被观测时间超过60s的时间中,哪个形状最多?
1.7.2.3 (b)对经纬度进行划分:-180°至180°以30°为一个划分,-90°至90°以18°为一个划分,请问哪个区域中报告的UFO事件数量最多?

1.3 第2章:练习一
1.3.1 (a)
df = pd.read_csv(‘data/UFO.csv’)
df.rename(columns={‘duration (seconds)’:‘duration’},inplace=True)
df[‘duration’].astype(‘float’)
df.head()
datetime shape duration latitude longitude
0 10/10/1949 20:30 cylinder 2700.0 29.883056 -97.941111
1 10/10/1949 21:00 light 7200.0 29.384210 -98.581082
2 10/10/1955 17:00 circle 20.0 53.200000 -2.916667
3 10/10/1956 21:00 circle 20.0 28.978333 -96.645833
4 10/10/1960 20:00 light 900.0 21.418056 -157.803611
df.query(‘duration > 60’)[‘shape’].value_counts().index[0]
‘light’
1.3.2 (b)
bins_long = np.linspace(-180,180,13).tolist()
bins_la = np.linspace(-90,90,11).tolist()
cuts_long = pd.cut(df[‘longitude’],bins=bins_long)
df[‘cuts_long’] = cuts_long
cuts_la = pd.cut(df[‘latitude’],bins=bins_la)
df[‘cuts_la’] = cuts_la
df.head()
datetime shape duration latitude longitude cuts_long cuts_la
0 10/10/1949 20:30 cylinder 2700.0 29.883056 -97.941111 (-120.0, -90.0] (18.0, 36.0]
1 10/10/1949 21:00 light 7200.0 29.384210 -98.581082 (-120.0, -90.0] (18.0, 36.0]
2 10/10/1955 17:00 circle 20.0 53.200000 -2.916667 (-30.0, 0.0] (36.0, 54.0]
3 10/10/1956 21:00 circle 20.0 28.978333 -96.645833 (-120.0, -90.0] (18.0, 36.0]
4 10/10/1960 20:00 light 900.0 21.418056 -157.803611 (-180.0, -150.0] (18.0, 36.0]
df.set_index([‘cuts_long’,‘cuts_la’]).index.value_counts().head()
((-90.0, -60.0], (36.0, 54.0]) 27891
((-120.0, -90.0], (18.0, 36.0]) 14280
((-120.0, -90.0], (36.0, 54.0]) 11960
((-90.0, -60.0], (18.0, 36.0]) 9923
((-150.0, -120.0], (36.0, 54.0]) 9658
dtype: int64

1.7.2.4 【练习二】 现有一份关于口袋妖怪的数据集,请解决下列问题:
pd.read_csv(‘data/Pokemon.csv’).head()

Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary

0 1 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False
4 4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False
1.7.2.5 (a)双属性的Pokemon占总体比例的多少?
1.7.2.6 (b)在所有种族值(Total)不小于580的Pokemon中,非神兽(Legendary=False)的比例为多少?
1.7.2.7 (c)在第一属性为格斗系(Fighting)的Pokemon中,物攻排名前三高的是哪些?
1.7.2.8 (d)请问六项种族指标(HP、物攻、特攻、物防、特防、速度)极差的均值最大的是哪个属性(只考虑第一属性,且均值是对属性而言)?
1.7.2.9 (e)哪个属性(只考虑第一属性)的神兽比例最高?该属性神兽的种族值也是最高的吗?

1.4 第2章:练习二
1.4.1 (a)
df = pd.read_csv(‘data/Pokemon.csv’)
df.head()

Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary

0 1 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
1 2 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False
2 3 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False
3 3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False
4 4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False
df[‘Type 2’].count()/df.shape[0]
0.5175
1.4.2 (b)
df.query(‘Total >= 580’)[‘Legendary’].value_counts(normalize=True)
True 0.575221
False 0.424779
Name: Legendary, dtype: float64
1.4.3 © 分别为Maga形态的路卡利欧、修缮老头、怪力
df[df[‘Type 1’]==‘Fighting’].sort_values(by=‘Attack’,ascending=False).iloc[:3]

Name Type 1 Type 2 Total HP Attack Defense Sp. Atk Sp. Def Speed Generation Legendary

498 448 LucarioMega Lucario Fighting Steel 625 70 145 88 140 70 112 4 False
594 534 Conkeldurr Fighting NaN 505 105 140 95 55 65 45 5 False
74 68 Machamp Fighting NaN 505 90 130 80 65 85 55 1 False
1.4.4 (d) 钢系的极差均值最大
df[‘range’] = df.iloc[:,5:11].max(axis=1)-df.iloc[:,5:11].min(axis=1)
attribute = df[[‘Type 1’,‘range’]].set_index(‘Type 1’)
max_range = 0
result = ‘’
for i in attribute.index.unique():
temp = attribute.loc[i,:].mean()
if temp.values[0] > max_range:
max_range = temp.values[0]
result = i
result
‘Steel’
1.4.5 (e) 超能系占比最高,但普通系均值最高(因为创世神的关系)
df.query(‘Legendary == True’)[‘Type 1’].value_counts(normalize=True).index[0]
‘Psychic’
attribute = df.query(‘Legendary == True’)[[‘Type 1’,‘Total’]].set_index(‘Type 1’)
max_value = 0
result = ‘’
for i in attribute.index.unique()[:-1]:
temp = attribute.loc[i,:].mean()
if temp[0] > max_value:
max_value = temp[0]
result = i
result
‘Normal’

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值