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 目录 ...
随机推荐
- WPF 中TextBox 增加输入检测,错误提示
先来总结下实现错误提示功能的几个要点 1:binding 的ValidationRules 2 :Validation.ErrorTemplate 首先我们在界面添加一个TextBox, Text绑定 ...
- 【C#】Enum,Int,String的互相转换 枚举转换
Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举. 注意:枚举类型的基 ...
- python下 conda命令手册
0.说明: 对于tensorflow配合keras使用,因为linux服务器没有root权限,所以目前最高可用版本是 1.6.0,否则就会报错某些 so找不到 conda install -n xu ...
- servlet中servletContext的五大作用(五)
1. 获取web的上下文路径 2. 获取全局的参数 3. 作为域对象使用 4. 请求转发 5. 读取web项目的资源文件 package day10.about_serv ...
- Python安装环境配置和多版本共存
Python安装环境配置和多版本共存 1.环境变量配置: (1) 右键点击"计算机",然后点击"属性" (2) 然后点击"高级系统设置" ( ...
- vue 点击复制当前网址
template 部分 <div class="NewNoticeDetails-ctrlButton" @click="copy()"> ...
- Linux(一)——简介
aaa https://www.cnblogs.com/three-fighter/p/14644152.html#navigator
- MySQL——select语句
select: 基本语法:select 列名 或 * from 对象(表.视图...) where: = > < <> != like ----> like 'old%' ...
- Python 高级特性(1)- 切片
前言 面 tx 被问到 python 的高级特性相关,这里做个补充学习吧 正向范围取值 关键点 首位下标是 0 第一个数字是起始下标,第二个数字是结束下标(但最终结果不包含它) 代码块一 # 正向范围 ...
- 通过JDK动态代理实现 Spring AOP
1.新建一个目标类 接口:public interface IUserService //切面编程 public void addUser(); public void updateUser( ); ...