python - pandas 1hour 速成
发布日期:2021-06-30 19:51:09 浏览次数:2 分类:技术文章

本文共 2160 字,大约阅读时间需要 7 分钟。

# coding: utf-8# ## numpy# In[169]:a = [1, 2, 3]b = [4, 5, 6]# In[170]:[f*s for f, s in zip(a, b)] # zip 返回tuple 的List# In[171]:import numpy as npna = np.array([    ['name', 'gender', 'age'],    ['frank', 'M', 32],    ['mary', 'F', 12],    ['tom', 'M', 21]])na# ## pandas# In[172]:import pandas as pddf = pd.DataFrame([    ['frank', 'M', 32],    ['mary', 'F', 12],    ['tom', 'M', 21]])df# In[173]:df.columns = ['name', 'gender', 'age']df# In[174]:df.head(2)# In[175]:df.tail(2)# In[176]:df.info() # 3组数据,3个变量# In[177]:df.describe()# ## series# In[178]:import pandas as pdphone = pd.Series([8100.0, 2500.0, 2350.0], index = ['Iphone 8', 'Oppo R11', 'Vivo x9'])phone# In[179]:phone[1]# In[180]:phone[0 : -1]# In[181]:phone['Iphone 8']# ## 切片# In[182]:df.ix[1]# In[183]:df['gender']# In[184]:df[['name', 'gender']]# In[185]:'''第一维:组第二维:变量名称'''df.ix[ 0:2, ['name', 'age']] # In[186]:'''补充说明:loc 在index的标签上进行索引,范围包括start和end. iloc 在index的位置上进行索引,不包括end. ix 先在index的标签上索引;索引不到就在index的位置上索引(如果index非全整数),并且不包括end.'''import pandas as pdimport numpy as nps = pd.Series(np.arange(5), index=[10, 23, 6, 16, 1])print(s)print(s.loc[23:16])print(s.iloc[1:3])# In[201]:# s.ix[:3] errors.ix[:1]# In[216]:s = pd.Series(np.arange(5), index=[10, 23, 5, 16, 'a'])# In[217]:s.ix[:5]# In[189]:'''利用 布尔值序列查询(筛选)数据'''(df['gender'] == 'M') & (df['age'] > 30)# In[190]:df[(df['gender'] == 'M') & (df['age'] > 30)]# In[191]:df[(df['gender'] == 'M') | (df['age'] > 30)]# In[192]:'''第二维,添加变量'''df['employee'] = Truedf# In[193]:'''第二维,删除变量'''del df['employee']df# In[194]:df['employee'] = Falsedf# In[195]:df.drop('employee', axis=1) # 第二维:axis=1,axis 默认为0# In[196]:df # drop返回一个新的df,而不是在本身进行操作# In[197]:# 添加1 组df.loc[3] = {'name' : 'lily', 'gender' : 'F', 'age' : 20, 'employee' : False}df# In[198]:# 添加n 组df = df.append(pd.DataFrame([    {'name' : 'lily', 'gender' : 'F', 'age' : 21, 'employee' : False},     {'name' : 'lily', 'gender' : 'F', 'age' : 22, 'employee' : False}]), ignore_index=True)df# In[199]:# 设置组号()df['uid'] = range(100, 106)df# In[200]:df.set_index('uid', inplace=True)df# In[203]:# 取值df.loc[[101, 103, 105]]# In[205]:df.iloc[[1, 3, 5]]# In[208]:df.ix[[100, 102, 104]]

转载地址:https://lipenglin.blog.csdn.net/article/details/77888267 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python - requests 1 hour 速成
下一篇:python - selenium 处理 frame

发表评论

最新留言

很好
[***.229.124.182]2024年04月18日 07时45分45秒