pandas 基础命令
import pandas as pd
import requests
import json
PATH = 'https://raw.githubusercontent.com/rmpbastos/data_sets/main/kaggle_housing/house_df.csv'
df = pd.read_csv(PATH)
type(df) # pandas.core.frame.DataFrame
df.head() # head 5
df.tail() # tail 5
df.shape # (1460, 16)
df.info() # summary of df
df.describe() # 描述性统计
df['Neighborhood'].value_counts() # count
# DataFrame index
df.set_index('Id', inplace=True)
df.index
df = pd.read_csv(PATH, index_col='Id') # second method
# rows and columns
df.columns
df['LotArea'].head()
type(df['LotArea']) # pandas.core.series.Series
df.rename(columns={'BedroomAbvGr': 'Bedroom'}, inplace=True) # rename columns
df_copy = df.copy() # copy dataframe
df_copy['Sold'] = 'N' # add column(s)
df_copy.tail()
data_to_append = {'LotArea': [9500, 15000],
'Steet': ['Pave', 'Gravel'],
'Neighborhood': ['Downtown', 'Downtown'],
'HouseStyle': ['2Story', '1Story'],
'YearBuilt': [2021, 2019],
'CentralAir': ['Y', 'N'],
'Bedroom': [5, 4],
'Fireplaces': [1, 0],
'GarageType': ['Attchd', 'Attchd'],
'GarageYrBlt': [2021, 2019],
'GarageArea': [300, 250],
'PoolArea': [0, 0],
'PoolQC': ['G', 'G'],
'Fence': ['G', 'G'],
'SalePrice': [250000, 195000],
'Sold': ['Y', 'Y']}
df_to_append = pd.DataFrame(data_to_append) # dict to dataframe
df_copy = df_copy.append(df_to_append, ignore_index=True) # add row(s)
df_copy.tail()
df_copy.drop(labels=1461, axis=0, inplace=True) # remove row(s) ; axis = 0
df_copy.drop(labels='Fence', axis=1, inplace=True) # remove column(s) ; axis = 1
# loc is used to access rows and columns by label/index or based on a boolean array
df.loc[1000] # the 1000th row; index = 1000
df.loc[1000, ['LotArea', 'SalePrice']] # index = 1000; columns = ['LotArea', 'SalePrice']
df.loc[df['SalePrice'] >= 600000] # df['SalePrice'] >= 600000 is condion; return boolen
# iloc is used to select data based on their integer location or based on a boolean array as well
df.iloc[0, 0] # 1st row; 1st column
df.iloc[10, :] # 10th column
df.iloc[:, -1] # the last colums
df.iloc[8:12, 2:5]
df.isnull() # detecting the missing values
df.isnull().sum() # the sum of missing values per column
df.isnull().sum() / df.shape[0] # ratio
# ratio > 0
for column in df.columns:
if df[column].isnull().sum() > 0:
print(column, ': {:.2%}'.format(df[column].isnull().sum() / df[column].shape[0]))
df_toremove = df.copy() # copy to drop
df_toremove.drop(labels=['PoolQC'], axis=1, inplace=True) # drop column(s)
df_toremove.dropna(subset=['GarageType'], axis=0, inplace=True) # drop rows
df_tofill = df.copy() # copy to fill the null
df_tofill['Fence'].fillna(value='NoFence', inplace=True) # fiil all in the column['Fence']
garage_median = df_tofill['GarageYrBlt'].median() # fill the median
df_tofill.fillna({'GarageYrBlt': garage_median}, inplace=True)
df['SalePrice'].plot(kind='hist'); # Histograms
df.plot(x='SalePrice', y='YearBuilt', kind='scatter') # scatter
df.to_csv(r'./Python_经济知识综合/My_DataFrame.csv') # save by the relative path
df.to_csv('C:/Users/username/Documents/My_DataFrame.csv') # absolute path
pandas 基础命令的更多相关文章
- Pandas基础学习与Spark Python初探
摘要:pandas是一个强大的Python数据分析工具包,pandas的两个主要数据结构Series(一维)和DataFrame(二维)处理了金融,统计,社会中的绝大多数典型用例科学,以及许多工程领域 ...
- Pandas 基础(1) - 初识及安装 yupyter
Hello, 大家好, 昨天说了我会再更新一个关于 Pandas 基础知识的教程, 这里就是啦......Pandas 被广泛应用于数据分析领域, 是一个很好的分析工具, 也是我们后面学习 machi ...
- 利用Python进行数据分析(12) pandas基础: 数据合并
pandas 提供了三种主要方法可以对数据进行合并: pandas.merge()方法:数据库风格的合并: pandas.concat()方法:轴向连接,即沿着一条轴将多个对象堆叠到一起: 实例方法c ...
- 利用Python进行数据分析(9) pandas基础: 汇总统计和计算
pandas 对象拥有一些常用的数学和统计方法. 例如,sum() 方法,进行列小计: sum() 方法传入 axis=1 指定为横向汇总,即行小计: idxmax() 获取最大值对应的索 ...
- 利用Python进行数据分析(8) pandas基础: Series和DataFrame的基本操作
一.reindex() 方法:重新索引 针对 Series 重新索引指的是根据index参数重新进行排序. 如果传入的索引值在数据里不存在,则不会报错,而是添加缺失值的新行. 不想用缺失值,可以用 ...
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- 学习 git基础命令
缘起 年后到了新公司,由于个人意愿到了一个海外的项目组,除了自己从Java技术栈转了C#技术栈外,很多技术都是第一次使用,学习压力不小啊. 自己也就先从常用的技术开始学起,比如C#,AngularJS ...
- Linux安全基础:shell及一些基础命令
1.什么是shell?Shell是用户和Linux操作系统之间的接口.Linux中有多种shell,其中缺省使用的是Bash. 2.shell的分类(1)bash bash shell 是 Bourn ...
- LINUX二十个基础命令
LINUX二十个基础命令 一. useradd命令 1.命令格式: useradd 选项 用户名 2.命令功能: 添加新的用户账号 3.常用参数: -c comment 指定一段注释性描述.-d 目录 ...
随机推荐
- mysql几种连接方式区别
mysql的几种join 2017年03月19日 14:49:07 carl-zhao 阅读数:7845 标签: mysqlsqljoin 更多 个人分类: MySQL 版权声明:本文为博主原创文章, ...
- 【java web】拦截器inteceptor
一.简介 java里的拦截器提供的是非系统级别的拦截,也就是说,就覆盖面来说,拦截器不如过滤器强大,但是更有针对性. Java中的拦截器是基于Java反射机制实现的,更准确的划分,应该是基于JDK实现 ...
- HttpClient4.3 教程 第五章 快速API
5.1.Easy to use facade API HttpClient从4.2开始支持快速api.快速api仅仅实现了HttpClient的基本功能,它只要用于一些不需要灵活性的简单场景.例如,快 ...
- C# WCF的POST请求包含Stream及多个参数
当使用WCF的API的POST请求时,如果参数列表里,除了Stream类型形参,还具有多个形参, 在寄宿过程中会报错: 约定"IService1"中的操作"DoWork& ...
- jQuery中的筛选(六):first()、last()、has()、is()、find()、siblings()等
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 简陋的Excel到MYSQL的数据传输JAVA实现
实现从excel读取数据,使用的是jxl.jar(到处都有,请大家随意下载),其中封装好了通过excel提供的接口,对excel中的数据库进行读取的实现: 先为了熟悉其中的方法使用,做了以下的测试: ...
- vue3.0入门(三)
前言 最近在b站上学习了飞哥的vue教程 学习案例已上传,下载地址 class绑定 对象绑定 :class='{active:isActive}' // 相当于class="active&q ...
- ES6扩展——正则扩展(u、y修饰符)
//下面三行代码效果一样 //全局匹配开头为a的 const regexp1 = /^a/g; const regexp2 = new RegExp('a','g'); const regexp3 = ...
- 去除所有js,html,css代码
<?php$search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript ...
- 从零开始实现简单 RPC 框架 7:网络通信之自定义协议(粘包拆包、编解码)
当 RPC 框架使用 Netty 通信时,实际上是将数据转化成 ByteBuf 的方式进行传输. 那如何转化呢?可不可以把 请求参数 或者 响应结果 直接无脑序列化成 byte 数组发出去? 答:直接 ...