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 ...
随机推荐
- javascript获取网页宽高,屏幕宽高,屏幕分辨率等
<script> var s = ""; s += "\r\n网页可见区域宽:"+ document.body.clientWidth; s + ...
- Linux对用户态的动态内存管理
Linux对内核态内存分配请求与用户态内存分配请求处理上分别对待 Linux本身信任自己,因此Linux内核请求分配多少内存,就会马上分配相同数量的内存出来. 但内核本身不相信应用程序,而且通常应用程 ...
- POJ1426-Find The Multiple-bfs
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal repr ...
- LightOJ 1418 Trees on My Island (Pick定理)
题目链接:LightOJ 1418 Problem Description I have bought an island where I want to plant trees in rows an ...
- JS高级程序随笔二
var person1={ toLoginString:function(){ return "lili"; }, toString2:function(){ return &qu ...
- Https socket 连接
介: 本文主要介绍了网络安全通讯协议 SSL/TLS 和 Java 中关于安全通讯的实现部分.并通过一个简单的样例程序实现,来展示如何在 Java 平台上正确建立安全通讯. 在人类建立了通信系统之后, ...
- PHP导出带有emoji表情的文本到excel文件出问题了
前段时间做了一个导出用户信息(包含微信昵称)到excel文件的功能,一直没问题,今天突然有人反馈说导出来的数据有一些丢失了.我试了一下,发现有些数据导出没问题,有些有问题,某些列出现了空白,数据打印出 ...
- 五、hibernate表与表之间的关系(一对多关系)
数据库表与表之间的关系 一对多:一个学校可以有多个学生,一个学生只能有一个学校 多对多:一个学生可以有多个老师,一个老师可以教多个学生 一对一:一个人只能有一个身份证号,一个身份证号只能找到一个人 一 ...
- elasticsearch Java Client用户指南
这里使用的Java客户端版本是5.1.2,Elasticsearch的版本号也要是5.1.2,否则一些功能可能不支持. 之前介绍过Spring Data Elasticsearch,那里也是使用了本文 ...
- Android中Parcelable的原理和使用方法
Parcelable的简单介绍 介绍Parcelable不得不先提一下Serializable接口,Serializable是Java为我们提供的一个标准化的序列化接口,那什么是序列化呢? 进行And ...