import pandas as pd
import numpy as np
s = pd.Series([1, 3, 6, np.nan, 44, 1]) df= pd.DataFrame(np.random.random((4,5))) # data frame 常用属性
df.dtypes
df.index
df.columns
df.values # data frame 常用方法
df.describe()
df.T
df.sort_index(axis = 1, ascending = False)
df.sort_values(by = 4) # 选择数据
dates = pd.date_range('', periods = 6)
df = pd.DataFrame(np.arange(24).reshape((6,4)), index = dates,
columns = ['A', 'B', 'C', 'D']) '''row or column''' # 行不可隔着选择
print(df[0:3])
print(df[['A', 'D']]) '''select by label:loc''' # 行不可隔着选择
print(df.loc['', :])
print(df.loc[:,['A', 'B']]) '''select by position:iloc'''
print(df.iloc[[0, 2], [0, 3]]) '''mixed selection:ix'''
print(df.ix[[0, 2], ['A', 'D']]) '''Boolean indexing'''
print(df[df.B > 5]) # 设置数据
df.iloc[2, 2] = 111
df.loc['', 'D'] = 222
df.B[df.A > 5] = 0
print(df) df['F'] = np.nan
df['E'] = range(6)
print(df) # 处理缺失数据
df.iloc[0, 1] = np.nan
df.iloc[1, 2] = np.nan
print(df)
print(df.dropna(axis = 0, how = 'all')) # how = {'any', 'all'}
print(df.fillna(value = 0))
print(np.any(df.isnull())) # data frame 合并
'''concatenating'''
df1 = pd.DataFrame(np.ones((3,4))*0, columns = ['a', 'b', 'c', 'd'])
df2 = pd.DataFrame(np.ones((3,4))*1, columns = ['a', 'b', 'c', 'd'])
df3 = pd.DataFrame(np.ones((3,4))*2, columns = ['a', 'b', 'c', 'd']) res = pd.concat([df1, df2, df3], axis = 0, ignore_index = True)
res1 = pd.concat([df1, df2, df3], axis = 1) '''join参数'''
df1 = pd.DataFrame(np.ones((3,4))*0, columns = ['a', 'b', 'c', 'd'], index = [1, 2, 3])
df2 = pd.DataFrame(np.ones((3,4))*1, columns = ['b', 'c', 'd', 'e'], index = [2, 3, 4]) res = pd.concat([df1, df2], join = 'outer', ignore_index = True)
res = pd.concat([df1, df2], join = 'inner', ignore_index = True)
print(res) '''join_axes'''
res = pd.concat([df1, df2], axis = 1, join = 'inner')
res = pd.concat([df1, df2], axis = 1, join_axes = [df1.index]) # append
df1 = pd.DataFrame(np.ones((3,4))*0, columns = ['a', 'b', 'c', 'd'], index = [1, 2, 3])
df2 = pd.DataFrame(np.ones((3,4))*1, columns = ['b', 'c', 'd', 'e'], index = [2, 3, 4])
df3 = pd.DataFrame(np.ones((3,4))*1, columns = ['b', 'c', 'd', 'e'], index = [2, 3, 4]) res = df1.append([df2, df3], ignore_index = True)
res1 = pd.concat([df1, df2, df3])
print(res)
print(res1) # data frame merge
'''merge one key'''
left = pd.DataFrame({'key':['K1','K2','K3'],
'A':[1,2,3],
'B':[4,5,6]}) right = pd.DataFrame({'key':['K0','K1','K3'],
'A':[11,43,53],
'D':[12,-1,0]})
res = pd.merge(left, right, on = 'key', how = 'outer')
print(res) '''merge two or more keys'''
left = pd.DataFrame({'key0':['K1','K2','K3'],
'key1':['X0','X2','X3'],
'A':[1,2,3],
'B':[4,5,6]}) right = pd.DataFrame({'key0':['K0','K1','K3'],
'key1':['X1','X0','K3'],
'A':[11,43,53],
'D':[12,-1,0]})
res = pd.merge(left, right, on = ['key0', 'key1'], how = 'outer')
print(res) '''merge index'''
left = pd.DataFrame({'A':[1,2,3],
'B':[4,5,6]},
index = ['K0', 'K1', 'K2']) right = pd.DataFrame({'A':[11,43,53],
'D':[12,-1,0]},
index = ['K1', 'K2', 'K3'])
res = pd.merge(left, right, left_index = True,
right_index = True)
print(res) '''handle overlapping columns'''
left = pd.DataFrame({'key':['K1','K2','K3'],
'A':[1,2,3],
'B':[4,5,6]}) right = pd.DataFrame({'key':['K0','K1','K3'],
'A':[11,43,53],
'B':[12,-1,0]})
res = pd.merge(left, right, on = 'key',
suffixes = ['_left', '_right'] , how = 'outer')
print(res) # 作图
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt '''plot data'''
'''Series'''
data = pd.Series(np.random.randn(1000), index = np.arange(1000))
data = data.cumsum()
data.plot()
print(data) '''Data Frame'''
data = pd.DataFrame(np.random.randn(1000, 4),
index = np.arange(1000),
columns = list("ABCD"))
print(data.head())
data = data.cumsum()
data.plot()
ax = data.plot.scatter(x = 'A', y = 'C',
color = 'Red',
label = 'Class 2')
data.plot.scatter(x = 'A', y = 'B',
color = 'DarkGreen',
label = 'Class 2',
ax = ax)

Python:Pandas学习的更多相关文章

  1. Python pandas学习总结

    本来打算学习pandas模块,并写一个博客记录一下自己的学习,但是不知道怎么了,最近好像有点急功近利,就想把别人的东西复制过来,当心沉下来,自己自觉地将原本写满的pandas学习笔记删除了,这次打算写 ...

  2. [IT学习]Python pandas 学习

    今天学习pandas来处理数据,结果用python 3.5.0的shell来调试,总是报错. 报错中包含如下字样: Traceback (most recent call last): File &q ...

  3. Python pandas学习笔记

    参考文献:<Python金融大数据分析> #导入模块 import pandas as pd #生成dataframe df = pd.DataFrame([10,20,30,40], c ...

  4. python 数据处理学习pandas之DataFrame

    请原谅没有一次写完,本文是自己学习过程中的记录,完善pandas的学习知识,对于现有网上资料的缺少和利用python进行数据分析这本书部分知识的过时,只好以记录的形势来写这篇文章.最如果后续工作定下来 ...

  5. Python Pandas库的学习(二)

    今天我们继续讲下Python中一款数据分析很好的库.Pandas的学习 接着上回讲到的,如果有人听不懂,麻烦去翻阅一下我前面讲到的Pandas学习(一) 如果我们在数据中,想去3,4,5这几行数据,那 ...

  6. Python: NumPy, Pandas学习资料

    NumPy 学习资料 书籍 NumPy Cookbook_[Idris2012] NumPy Beginner's Guide,3rd_[Idris2015] Python数据分析基础教程:NumPy ...

  7. 用scikit-learn和pandas学习线性回归

    对于想深入了解线性回归的童鞋,这里给出一个完整的例子,详细学完这个例子,对用scikit-learn来运行线性回归,评估模型不会有什么问题了. 1. 获取数据,定义问题 没有数据,当然没法研究机器学习 ...

  8. Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习

    http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...

  9. Python人工智能学习笔记

    Python教程 Python 教程 Python 简介 Python 环境搭建 Python 中文编码 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 ...

  10. pandas学习笔记(一)

    Pandas是一款开放源码的BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Pandas用于广泛的领域,包括金融,经济,统计,分析等学术和商业领域.在 ...

随机推荐

  1. 为什么java的web开发中URLEncoder.encode方法要为什么要调用两次

    一: 我们先看2个编码的情况 String name=java.net.URLEncoder.encode("测试", "UTF-8"); System.out ...

  2. 多线程中的lock,Monitor.Wait和Monitor.Pulse

    我们知道lock实际上一个语法糖糖,C#编译器实际上把他展开为Monitor.Enter和Monitor.Exit,即: lock(lockObj) { //... } ////相当于(.Net4以前 ...

  3. C++技术问题总结-第0篇 类型转换

    从今天開始,对C++经常使用技术做个总结. 參考书籍:<C++Primer>.<C++对象模型>.<设计模式>.<Windows核心编程>.<ST ...

  4. Configuring a remote m-phy

    An interface for low power, high bandwidth communications between units in a device in provided here ...

  5. End-to end provisioning of storage clouds

    Embodiments discussed in this disclosure provide an integrated provisioning framework that automates ...

  6. 写一个去除AI2XAML注释及多余数字位数的WPF窗体程序

    原文:写一个去除AI2XAML注释及多余数字位数的WPF窗体程序 使用正则表达式去除多余注释及冗余数字位,关键代码:            string pattern = @"/b(/d+ ...

  7. Windows中点击“关闭”button发生了什么?

    对于Windows操作,当用户点击"关闭"button时,窗体函数就会收到一个WM_DESTROY消息. 窗体函数应该调用PostQuitMessage(0) 向消息队列插入一个W ...

  8. ASP.NET Core Razor 视图导入 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Razor 视图导入 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Razor 视图导入 上一章节我们介绍了视图起始页,学习 ...

  9. modern-cpp-features

    C++17/14/11 Overview Many of these descriptions and examples come from various resources (see Acknow ...

  10. LeapMotion Demo2

    原文:LeapMotion Demo2    官方doc有四个手势,最近尝试实现对握拳的识别,并能在我的程序界面上体现出来.    调试过程较为繁琐,幸好最终效果还差强人意! 首先看看我的效果图:   ...