Pandas DataFrame操作
DataFrame的创建
>>> import pandas as pd
>>> from pandas import DataFrame
#define a dict
>>> dic = {'Name':['Jeff','Lucy','Evan'],'Age':[28,26,27],'Sex':['Male','Female','Male']}
Load the dict to the dataframe
>>> df = DataFrame(dic)
>>> print df
Age Name Sex
0 28 Jeff Male
1 26 Lucy Female
2 27 Evan Male
#the order of the columns is default #We define the order
>>> df1 = DataFrame(dic,columns=['Name','Sex','Age'])
>>> df1
Name Sex Age
0 Jeff Male 28
1 Lucy Female 26
2 Evan Male 27 #Define an empty column
>>> df1 = DataFrame(dic,columns=['Name','Age','Sex','Major'])
>>> df1
Name Age Sex Major
0 Jeff 28 Male NaN
1 Lucy 26 Female NaN
2 Evan 27 Male NaN #Define the row name
>>> df1 = DataFrame(dic,columns=['Name','Age','Sex','Major'],index=['one','two','three'])
>>> df1
Name Age Sex Major
one Jeff 28 Male NaN
two Lucy 26 Female NaN
three Evan 27 Male NaN
DataFrame内容读取与改变
>>> df1.columns
Index([u'Name', u'Age', u'Sex', u'Major'], dtype='object')
>>> df1.Sex
one Male
two Female
three Male
Name: Sex, dtype: object >>> df1['Sex']
one Male
two Female
three Male
Name: Sex, dtype: object >>> df1.ix['two']
Name Lucy
Age 26
Sex Female
Major NaN
Name: two, dtype: object >>> df1.index
Index([u'one', u'two', u'three'], dtype='object') #Copy a colum from a Series
>>> df1
Name Age Sex Major
one Jeff 28 Male NaN
two Lucy 26 Female NaN
three Evan 27 Male NaN
>>> s1 = (['Se','Se','Ce'])
>>> df1.Major=s1
>>> df1
Name Age Sex Major
one Jeff 28 Male Se
two Lucy 26 Female Se
three Evan 27 Male Ce #Define a new column
>>> df1['Type']=df1.Major=='Se'
>>> df1
Name Age Sex Major Type
one Jeff 28 Male Se True
two Lucy 26 Female Se True
three Evan 27 Male Ce False #Remove a column
>>> del df1['Type']
>>> df1
Name Age Sex Major
one Jeff 28 Male Se
two Lucy 26 Female Se
three Evan 27 Male Ce
Other Methods to define
Define a DF with Two-layer Dict
>>> dic1={'name':{'1':'Jeff','2':'Mia','3':'Evan'},'age':{'1':28,'3':27,'2':18,'4':23}}
>>> df2=DataFrame(dic1)
>>> df2
age name
1 28 Jeff
2 18 Mia
3 27 Evan
4 23 NaN Transpose
>>> df2.T
1 2 3 4
age 28 18 27 23
name Jeff Mia Evan NaN >>> df2.columns.name='items'
>>> df2.index.name='student_id'
>>> df2
items age name
student_id
1 28 Jeff
2 18 Mia
3 27 Evan
4 23 NaN >>> df2.values
array([[28L, 'Jeff'],
[18L, 'Mia'],
[27L, 'Evan'],
[23L, nan]], dtype=object)
Pandas DataFrame操作的更多相关文章
- Python pandas DataFrame操作
1. 从字典创建Dataframe >>> import pandas as pd >>> dict1 = {'col1':[1,2,5,7],'col2':['a ...
- 数据清理,预处理 pandas dataframe 操作技巧 总结
dsoft2 = data1.loc[(data1['程'] == "轻") | (data1['程'] == "中")]设置x下标plt.xticks(np. ...
- python pandas dataframe 操作记录
从数据看select出数据后如何转换为dataframe df = DataFrame(cur.fetchall()) 如何更改列名,选取列,进行groupby操作 df.columns = ['me ...
- pandas基础:Series与DataFrame操作
pandas包 # 引入包 import pandas as pd import numpy as np import matplotlib.pyplot as plt Series Series 是 ...
- pandas DataFrame 数据处理常用操作
Xgboost调参: https://wuhuhu800.github.io/2018/02/28/XGboost_param_share/ https://blog.csdn.net/hx2017/ ...
- Python时间处理,datetime中的strftime/strptime+pandas.DataFrame.pivot_table(像groupby之类 的操作)
python中datetime模块非常好用,提供了日期格式和字符串格式相互转化的函数strftime/strptime 1.由日期格式转化为字符串格式的函数为: datetime.datetime.s ...
- pandas.DataFrame的pivot()和unstack()实现行转列
示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...
- pandas数据操作
pandas数据操作 字符串方法 Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素 t = pd.Series(['a_b_c_d','c_d_e',np. ...
- 如何迭代pandas dataframe的行
from:https://blog.csdn.net/tanzuozhev/article/details/76713387 How to iterate over rows in a DataFra ...
随机推荐
- CentOS下Java的安装与环境配置
网上的文章很多,但我还是不知道下次需要看谁的,或是给朋友推荐谁的,索性我都整理出来,方便下次需要的时候能很快的看到或是给朋友链接.两种安装方式:解压安装和包安装 1.安装前检查: 因为安 ...
- C#进阶系列——WebApi 路由机制剖析:你准备好了吗? 转载https://www.cnblogs.com/landeanfen/p/5501490.html
阅读目录 一.MVC和WebApi路由机制比较 1.MVC里面的路由 2.WebApi里面的路由 二.WebApi路由基础 1.默认路由 2.自定义路由 3.路由原理 三.WebApi路由过程 1.根 ...
- drf 分页,获取fk,choise,m2m等字段数据(序列化)
1.什么是restful规范 是一套规则,用于程序之间进行数据交换的约定. 他规定了一些协议,对我们感受最直接的的是,以前写增删改查需要写4个接口,restful规范的就是1个接口,根据method的 ...
- PAT 2019-3 7-3 Telefraud Detection
Description: Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, ...
- git使用记录七:对工作区和暂存区的一些操作场景
比较暂存区和HEAD所含文件的差异? 操作场景如下: 修改readme.md 文档 vi readme.md 加入到暂存区域 git add readme.md 使用git diff -cached ...
- C++中的抽象类和接口
1,在 C++ 语言中,并不直接支持面向对象中的抽象类和接口概念,但是 C++ 语言 却可以间接实现这些概念: 2,什么是抽象类: 1,面向对象中的抽象(其实就是分类)概念: 1,在进行面向对象分析时 ...
- Django上线部署之uWSGI
环境: 1.CentOS 7.2 64位 2.SQL Server 2016 Enterprise 64位 3.Python 3.6.5 64位 4.root用户 要求: 按照顺序部署 1.Windo ...
- P3391 文艺平衡树(Splay)
题目背景 这是一道经典的Splay模板题--文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- JS面向对象——构造函数模型
ECMAScript中的构造函数可用来创建特定类型的对象.我们可以创建自定义构造函数,从而定义对象类型的属性和方法,解决工厂模型中对象识别的问题. <!DOCTYPE html> < ...
- ArrayListMultimap
遇到这样一个场景,就是要判断传过来的Lists里有几组相邻的元素,然后有多少个单一的元素,比如3,3,3,4,4,4,5,5,5,6,6,6,8,9,10,11 方法有很多,但是我选了ArrayLis ...