Pandas学习打卡日记#2

import numpy as np
import pandas as pd
df = pd.read_csv('F:/data/UFO.csv')
df.head()
datetimeshapeduration (seconds)latitudelongitude
010/10/1949 20:30cylinder2700.029.883056-97.941111
110/10/1949 21:00light7200.029.384210-98.581082
210/10/1955 17:00circle20.053.200000-2.916667
310/10/1956 21:00circle20.028.978333-96.645833
410/10/1960 20:00light900.021.418056-157.803611
df.rename(columns = {'duration (seconds)':'duration'} , inplace =  True)
df.head()
datetimeshapedurationlatitudelongitude
010/10/1949 20:30cylinder2700.029.883056-97.941111
110/10/1949 21:00light7200.029.384210-98.581082
210/10/1955 17:00circle20.053.200000-2.916667
310/10/1956 21:00circle20.028.978333-96.645833
410/10/1960 20:00light900.021.418056-157.803611
df.loc[lambda x : x['duration'] > 60]['shape'].value_counts().index[0]
'light'
interval1 = pd.interval_range(start = -180 , end = 180 , freq = 30)
interval2 = pd.interval_range(start = -90 , end = 90 , freq = 18)
IntervalIndex([(-90, -72], (-72, -54], (-54, -36], (-36, -18], (-18, 0], (0, 18], (18, 36], (36, 54], (54, 72], (72, 90]],
              closed='right',
              dtype='interval[int64]')
cuts1 = pd.cut(df['longitude'],bins = interval1)
cuts2 = pd.cut(df['latitude'],bins = interval2)
df['longitude'] = cuts1
df['latitude'] = cuts2
df.head()
datetimeshapedurationlatitudelongitude
010/10/1949 20:30cylinder2700.0(18, 36](-120, -90]
110/10/1949 21:00light7200.0(18, 36](-120, -90]
210/10/1955 17:00circle20.0(36, 54](-30, 0]
310/10/1956 21:00circle20.0(18, 36](-120, -90]
410/10/1960 20:00light900.0(18, 36](-180, -150]
df.set_index([cuts1,cuts2]).index.value_counts().head().head()
((-90, -60], (36, 54])      27891
((-120, -90], (18, 36])     14280
((-120, -90], (36, 54])     11960
((-90, -60], (18, 36])       9923
((-150, -120], (36, 54])     9658
dtype: int64
#作业二

df2 = pd.read_csv('F:/data/Pokemon.csv')
df2.head()
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
01BulbasaurGrassPoison3184549496565451False
12IvysaurGrassPoison4056062638080601False
23VenusaurGrassPoison525808283100100801False
33VenusaurMega VenusaurGrassPoison62580100123122120801False
44CharmanderFireNaN3093952436050651False
df2['Type 2'].count()/df2.shape[0]
0.5175
df2.query('Total >= 580')['Legendary'].value_counts(normalize = True)
True     0.575221
False    0.424779
Name: Legendary, dtype: float64
df_temp = df2.loc[lambda x : x['Type 1']=='Fighting']
df_temp.sort_values(by='Attack',ascending=False).head(3)
#NameType 1Type 2TotalHPAttackDefenseSp. AtkSp. DefSpeedGenerationLegendary
498448LucarioMega LucarioFightingSteel6257014588140701124False
594534ConkeldurrFightingNaN505105140955565455False
7468MachampFightingNaN50590130806585551False
df2['range'] = df2.iloc[:,5:11].max(axis=1)-df2.iloc[:,5:11].min(axis=1)
df_temp = df2[['Type 1','range']].set_index('Type 1')
mrg=0
result = ''
for i in df_temp.index.unique():
    temp = df_temp.loc[i,:].mean()
    if temp.values[0] > mrg:
        mrg = temp.values[0]
        result = i
result
'Steel'
df2.query('Legendary == True')['Type 1'].value_counts().index[0]
'Psychic'
df_temp = df2.query('Legendary == True')[['Type 1','Total']].set_index('Type 1')
mval = 0
result = ''
for i in df_temp.index.unique():
    temp = float(df_temp.loc[i,:].mean())
    if temp > mval:
        mval = temp
        result = i
result
'Normal'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值