首页 > 其他 > 详细

numpy和pandas 小计

时间:2020-11-05 21:22:35      阅读:34      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/python
# -*- coding:utf-8 -*-

import numpy as np
import pandas as pd


#用值列表生成 Series 时,Pandas 默认自动生成整数索引
s = pd.Series([1,3,5,np.nan,6,8])
print s

#用含日期时间索引与标签的 NumPy 数组生成 DataFrame :
dates = pd.date_range(20200901,periods=6)
print dates

df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list(ABCD))
print df

#用 Series 字典对象生成 DataFrame:
df2 = pd.DataFrame({A: 1.,
                    B: pd.Timestamp(20130102),
                    C: pd.Series(1, index=list(range(4)), dtype=float32),
                    D: np.array([3] * 4, dtype=int32),
                    E: pd.Categorical(["test", "train", "test", "train"]),
                    F: foo})

print df2

#DataFrame 的列有不同数据类型 。
print df2.dtypes


#查看 DataFrame 头部和尾部数据:
print df.head()
print df.tail(2)


#显示索引与列名:
print df.index
print df.columns

#可以快速查看数据的统计摘要
print df.describe()

print df

#转置数据
print df.T

#按轴排序
print df.sort_index(axis=1, ascending=False)

#按值排序
print df.sort_values(by=B)

#选择单列,产生 Series,与 df.A 等效:
print  df[A]

#用 [ ] 切片行
print df[0:3]

#用标签提取一行数据
print df.loc[dates[0]]
print df.loc[dates[1]]


#用标签选择多列数据
print df.loc[:, [A, B]]

#用标签切片,包含行与列结束点
print df.loc[2020-09-01:2020-09-02, [A, B]]

#返回对象降维
print df.loc[2020-09-02, [A, B]]

#提取标量值
print df.loc[dates[0], A]
print df.loc[dates[1], A]

print df.at[dates[0], A]
print df.at[dates[1], A]

#用整数位置选择
print df
print df.iloc[3]

#用整数切片
df.iloc[3:5, 0:2]

#用整数列表按位置切片
df.iloc[[1, 2, 4], [0, 2]]

#显式整行切片
df.iloc[1:3, :]

#显式整列切片
df.iloc[:, 1:3]

#显式提取值
df.iloc[1, 1]

--布尔索引
#用单列的值选择数据
df[df.A > 0]

#选择 DataFrame 里满足条件的值
df[df > 0]


#用 isin() 筛选
df2 = df.copy()
df2[E] = [one, one, two, three, four, three]
df2[df2[E].isin([two, four])]



--赋值
s1 = pd.Series([1, 2, 3, 4, 5, 6], index=pd.date_range(20200901, periods=6))
df[F] = s1

#按标签赋值
df.at[dates[0], A] = 0
df

#按位置赋值
df.iat[0, 1] = 0
df

#按 NumPy 数组赋值
df.loc[:, D] = np.array([5] * len(df))
df


#用 where 条件赋值:
df2 = df.copy()
df2[df2 > 0] = -df2
df2


--缺失值
#Pandas 主要用 np.nan 表示缺失数据。 计算时,默认不包含空值
#重建索引(reindex)可以更改、添加、删除指定轴的索引,并返回数据副本,即不更改原数据
df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + [E])
df1.loc[dates[0]:dates[1], E] = 1
df1


#删除所有含缺失值的行
df1.dropna(how=any)


#填充缺失值
df1.fillna(value=5)


#提取 nan 值的布尔掩码
pd.isna(df1)
 

--运算
#描述性统计
df.mean()


#在另一个轴(即,行)上执行同样的操作
df.mean(1)

#不同维度对象运算时,要先对齐。 此外,Pandas 自动沿指定维度广播  shift下移几位
s = pd.Series([1, 3, 5, np.nan, 6, 8], index=dates).shift(2) 


#pply 函数处理数据
df.apply(np.cumsum)
df.apply(lambda x: x.max() - x.min())


--直方图
#直方图
s = pd.Series(np.random.randint(0, 7, size=10))
s.value_counts()



--字符串方法
#Series 的 str 属性包含一组字符串处理功能,如下列代码所示。注意,str 的模式匹配默认使用正则表达式
s = pd.Series([A, B, C, Aaba, Baca, np.nan, CABA, dog, cat])
s.str.lower()


--合并(Merge)
结合(Concat)
#Pandas 提供了多种将 Series、DataFrame #对象组合在一起的功能,用索引与关联代数功能的多种设置逻辑可执行连接(join)与合并(merge)操作
df = pd.DataFrame(np.random.randn(10, 4))
pieces = [df[:3], df[3:7], df[7:]]
pd.concat(pieces)

 

numpy和pandas 小计

原文:https://www.cnblogs.com/hello-wei/p/13933227.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!