参考链接:https://github.com/rmpbastos/data_science/blob/master/_0014_Boost_your_Data_Analysis_with_Pandas.ipynb

  1. import pandas as pd
  2. import requests
  3. import json
  4. PATH = 'https://raw.githubusercontent.com/rmpbastos/data_sets/main/kaggle_housing/house_df.csv'
  5. df = pd.read_csv(PATH)
  6. type(df) # pandas.core.frame.DataFrame
  7. df.head() # head 5
  8. df.tail() # tail 5
  9. df.shape # (1460, 16)
  10. df.info() # summary of df
  11. df.describe() # 描述性统计
  12. df['Neighborhood'].value_counts() # count
  13. # DataFrame index
  14. df.set_index('Id', inplace=True)
  15. df.index
  16. df = pd.read_csv(PATH, index_col='Id') # second method
  17. # rows and columns
  18. df.columns
  19. df['LotArea'].head()
  20. type(df['LotArea']) # pandas.core.series.Series
  21. df.rename(columns={'BedroomAbvGr': 'Bedroom'}, inplace=True) # rename columns
  22. df_copy = df.copy() # copy dataframe
  23. df_copy['Sold'] = 'N' # add column(s)
  24. df_copy.tail()
  25. data_to_append = {'LotArea': [9500, 15000],
  26. 'Steet': ['Pave', 'Gravel'],
  27. 'Neighborhood': ['Downtown', 'Downtown'],
  28. 'HouseStyle': ['2Story', '1Story'],
  29. 'YearBuilt': [2021, 2019],
  30. 'CentralAir': ['Y', 'N'],
  31. 'Bedroom': [5, 4],
  32. 'Fireplaces': [1, 0],
  33. 'GarageType': ['Attchd', 'Attchd'],
  34. 'GarageYrBlt': [2021, 2019],
  35. 'GarageArea': [300, 250],
  36. 'PoolArea': [0, 0],
  37. 'PoolQC': ['G', 'G'],
  38. 'Fence': ['G', 'G'],
  39. 'SalePrice': [250000, 195000],
  40. 'Sold': ['Y', 'Y']}
  41. df_to_append = pd.DataFrame(data_to_append) # dict to dataframe
  42. df_copy = df_copy.append(df_to_append, ignore_index=True) # add row(s)
  43. df_copy.tail()
  44. df_copy.drop(labels=1461, axis=0, inplace=True) # remove row(s) ; axis = 0
  45. df_copy.drop(labels='Fence', axis=1, inplace=True) # remove column(s) ; axis = 1
  46. # loc is used to access rows and columns by label/index or based on a boolean array
  47. df.loc[1000] # the 1000th row; index = 1000
  48. df.loc[1000, ['LotArea', 'SalePrice']] # index = 1000; columns = ['LotArea', 'SalePrice']
  49. df.loc[df['SalePrice'] >= 600000] # df['SalePrice'] >= 600000 is condion; return boolen
  50. # iloc is used to select data based on their integer location or based on a boolean array as well
  51. df.iloc[0, 0] # 1st row; 1st column
  52. df.iloc[10, :] # 10th column
  53. df.iloc[:, -1] # the last colums
  54. df.iloc[8:12, 2:5]
  55. df.isnull() # detecting the missing values
  56. df.isnull().sum() # the sum of missing values per column
  57. df.isnull().sum() / df.shape[0] # ratio
  58. # ratio > 0
  59. for column in df.columns:
  60. if df[column].isnull().sum() > 0:
  61. print(column, ': {:.2%}'.format(df[column].isnull().sum() / df[column].shape[0]))
  62. df_toremove = df.copy() # copy to drop
  63. df_toremove.drop(labels=['PoolQC'], axis=1, inplace=True) # drop column(s)
  64. df_toremove.dropna(subset=['GarageType'], axis=0, inplace=True) # drop rows
  65. df_tofill = df.copy() # copy to fill the null
  66. df_tofill['Fence'].fillna(value='NoFence', inplace=True) # fiil all in the column['Fence']
  67. garage_median = df_tofill['GarageYrBlt'].median() # fill the median
  68. df_tofill.fillna({'GarageYrBlt': garage_median}, inplace=True)
  69. df['SalePrice'].plot(kind='hist'); # Histograms
  70. df.plot(x='SalePrice', y='YearBuilt', kind='scatter') # scatter
  71. df.to_csv(r'./Python_经济知识综合/My_DataFrame.csv') # save by the relative path
  72. df.to_csv('C:/Users/username/Documents/My_DataFrame.csv') # absolute path

pandas 基础命令的更多相关文章

  1. Pandas基础学习与Spark Python初探

    摘要:pandas是一个强大的Python数据分析工具包,pandas的两个主要数据结构Series(一维)和DataFrame(二维)处理了金融,统计,社会中的绝大多数典型用例科学,以及许多工程领域 ...

  2. Pandas 基础(1) - 初识及安装 yupyter

    Hello, 大家好, 昨天说了我会再更新一个关于 Pandas 基础知识的教程, 这里就是啦......Pandas 被广泛应用于数据分析领域, 是一个很好的分析工具, 也是我们后面学习 machi ...

  3. 利用Python进行数据分析(12) pandas基础: 数据合并

    pandas 提供了三种主要方法可以对数据进行合并: pandas.merge()方法:数据库风格的合并: pandas.concat()方法:轴向连接,即沿着一条轴将多个对象堆叠到一起: 实例方法c ...

  4. 利用Python进行数据分析(9) pandas基础: 汇总统计和计算

    pandas 对象拥有一些常用的数学和统计方法.   例如,sum() 方法,进行列小计:   sum() 方法传入 axis=1 指定为横向汇总,即行小计:   idxmax() 获取最大值对应的索 ...

  5. 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作

    一.reindex() 方法:重新索引 针对 Series   重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...

  6. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  7. 学习 git基础命令

    缘起 年后到了新公司,由于个人意愿到了一个海外的项目组,除了自己从Java技术栈转了C#技术栈外,很多技术都是第一次使用,学习压力不小啊. 自己也就先从常用的技术开始学起,比如C#,AngularJS ...

  8. Linux安全基础:shell及一些基础命令

    1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...

  9. LINUX二十个基础命令

    LINUX二十个基础命令 一. useradd命令 1.命令格式: useradd 选项 用户名 2.命令功能: 添加新的用户账号 3.常用参数: -c comment 指定一段注释性描述.-d 目录 ...

随机推荐

  1. 安全|常见的Web攻击手段之CSRF攻击

    对于常规的Web攻击手段,如XSS.CRSF.SQL注入.(常规的不包括文件上传漏洞.DDoS攻击)等,防范措施相对来说比较容易,对症下药即可,比如XSS的防范需要转义掉输入的尖括号,防止CRSF攻击 ...

  2. SpringBoot中的静态资源访问

    一.说在前面的话 我们之间介绍过SpringBoot自动配置的原理,基本上是如下: xxxxAutoConfiguration:帮我们给容器中自动配置组件: xxxxProperties:配置类来封装 ...

  3. SQLServer 判断文件是否存在

    根据20190621工作写的逻辑,以后可根据实际情况再做修改.顺便记录一下游标的使用,加强记忆. DECLARE @Id NVARCHAR(MAX) DECLARE @UserName NVARCHA ...

  4. 关于Eclipse中使用Maven进行Install安装时候报错Perhaps you are running on a JRE rather than a JDK?解决办法

    所遇到的问题: 详情报错: 英文描述: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3. ...

  5. win系统打不开CHM文件(例如JDK的API)

    打开文件乱码,打开时弹出乱码 前提说明,存放路径不得有中文,文件名也不能有中文 检查下面三个文件:    hh.exe文件放置电脑 C:\Windows目录下:     hhctrl.ocx ,its ...

  6. maven下载出错

    求解

  7. GoLang设计模式3 - 抽象工厂模式

    之前我们介绍了工厂设计模式,现在我们再看一下抽象工厂设计模式.抽象工程模式顾名思义就是对工厂模式的一层抽象,也是创建型模式的一种,通常用来创建一组存在相关性的对象. UML类图大致如下: 类图比较复杂 ...

  8. Redis详解(二)——

    https://www.cnblogs.com/yeya/p/14274948.html https://www.cnblogs.com/liang24/tag/redis/

  9. MySQL(一)——入门

    一.安装 二.配置环境变量 https://www.cnblogs.com/wzk153/category/1934516.html https://www.cnblogs.com/wzk153/ca ...

  10. Django——ORM打印SQL

    如果想打印ORM转换过程中的SQL,需要在settings.py中进行如下配置: LOGGING = { 'version': 1, 'disable_existing_loggers': False ...