Pandas各种方式读取dataframe的数据,有这篇就够了
###### loc只能通过index和columns的名称来取,不能用数字
########iloc只能用数字索引,不能用索引名
import numpy as np
from pandas import DataFrame
import pandas as pd
#构建dataframe,二维数组
df = DataFrame(np.arange(20).reshape(4,5),index=['one','two','three','four'],columns=list('abcde'))
print(df)
a b c d e
one 0 1 2 3 4
two 5 6 7 8 9
three 10 11 12 13 14
four 15 16 17 18 19
############### 1.获取指定列的数据######
print("获取指定的某列:")
print(df['a'])
#注意双[]
print(df[['a','b']])
获取指定的某列:
one 0
two 5
three 10
four 15
Name: a, dtype: int32
a b
one 0 1
two 5 6
three 10 11
four 15 16
############### 2.获取指行的数据######
print("获取指行的某行:")
print(df.loc['one'])
print(df.iloc[0:1])
获取指行的某行:
a 0
b 1
c 2
d 3
e 4
Name: one, dtype: int32
a b c d e
one 0 1 2 3 4
############### 3.loc获取指某行某列的数据######
###### loc只能通过index和columns的名称来取,不能用数字
print("loc获取指定某行某列的数据")
print(df.loc['one','c'])
#获取行索引one到three,列索引a到d的数据
print(df.loc['one':"three","a":'d'])
#获取行索引one到three,列索引a,d的数据
print(df.loc['one':'three',['a','d']])
#获取行索引one,three,列索引a,d的数据
print(df.loc[['one','three'],['a','d']])
loc获取指定某行某列的数据
2
a b c d
one 0 1 2 3
two 5 6 7 8
three 10 11 12 13
a d
one 0 3
two 5 8
three 10 13
a d
one 0 3
three 10 13
############### 4.iloc获取指某行某列的数据######
########iloc只能用数字索引,不能用索引名
print("iloc获取指定某行某列的数据")
print(df.iloc[0:1,2:3])
#获取行索引one到three,列索引a到d的数据
print(df.iloc[0:4,0:4])
#获取行索引one到three,列索引a,d的数据
print(df.iloc[0:4,[0,3]])
#获取行索引one,three,列索引a,d的数据
print(df.iloc[[0,2],[0,3]])
iloc获取指定某行某列的数据
c
one 2
a b c d
one 0 1 2 3
two 5 6 7 8
three 10 11 12 13
four 15 16 17 18
a d
one 0 3
two 5 8
three 10 13
four 15 18
a d
one 0 3
three 10 13
############### 5.获取具体某个单值######
#iat取某个单值,只能数字索引
#at取某个单值,只能index和columns索引
print(df.iat[0,1])
print(df.at['one','b'])
1
1
相关文章
- 简单又强大的pandas爬虫 利用pandas库的read_html()大法爬取网页表格型数据
- pandas——DataFrame基本操作(二)【建议收藏】
- Pandas-数据结构-MultiIndex(老版本中叫Panel ):三维的数据结构【弃用】【推荐的用于表示3D数据的方法是通过DataFrame上的MultiIndex方法】
- Pandas处理大数据的一些小技巧
- python数据分析之pandas数据选取:df[] df.loc[] df.iloc[] df.ix[] df.at[] df.iat[]
- 数据分析-day03-pandas-dataFrame获取dataframe某行某列的数据
- 数据的简单处理:pandas模块入门
- pandas数据框,统计某列或者某行数据元素的个数
- GIS+=地理信息+大数据——Windows部署Pandas环境及代码測试验证
- 【pandas】数据去重
- pandas教程
- python/pandas数据挖掘(十四)-groupby,聚合,分组级运算
- 已解决pandas无法读取中文文件的问题
- Python pandas.DataFrame.get_dtype_counts函数方法的使用
- Python pandas 合并两个或多个DataFrame的方法代码
- Python pandas.DataFrame.copy函数方法的使用
- Python Pandas pandas.DataFrame.to_html函数方法的使用
- 数据导入与预处理-拓展-pandas时间数据处理02
- 猿创征文|数据导入与预处理-第3章-pandas基础
- pandas操作Excel