pandas基础2
先写练习题,明天再补知识点,好困,又是一个flag
1、(a)
df = pd.read_csv('data/UFO.csv')
df.rename(columns={'duration (seconds)':'duration'},inplace=True)
df['duration'].astype('float')
df.head()
df.query('duration > 60')['shape'].value_counts().index[0]
(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()
df.set_index(['cuts_long','cuts_la']).index.value_counts().head()
2、(a)
df = pd.read_csv('data/Pokemon.csv')
df.head()
df['Type 2'].count()/df.shape[0]
(b)
df.query('Total >= 580')['Legendary'].value_counts(normalize=True)
(c)
df[df['Type 1']=='Fighting'].sort_values(by='Attack',ascending=False).iloc[:3]
(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
(e)
df.query('Legendary == True')['Type 1'].value_counts(normalize=True).index[0]
attribute = df.query('Legendary == True')[['Type 1','Total']].set_index('Type 1')
max_value = 0
result = ''
for i in attribute.index.unique():
temp = float(attribute.loc[i,:].mean())
if temp > max_value:
max_value = temp
result = i
result