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操作的更多相关文章

  1. Python pandas DataFrame操作

    1. 从字典创建Dataframe >>> import pandas as pd >>> dict1 = {'col1':[1,2,5,7],'col2':['a ...

  2. 数据清理,预处理 pandas dataframe 操作技巧 总结

    dsoft2 = data1.loc[(data1['程'] == "轻") | (data1['程'] == "中")]设置x下标plt.xticks(np. ...

  3. python pandas dataframe 操作记录

    从数据看select出数据后如何转换为dataframe df = DataFrame(cur.fetchall()) 如何更改列名,选取列,进行groupby操作 df.columns = ['me ...

  4. pandas基础:Series与DataFrame操作

    pandas包 # 引入包 import pandas as pd import numpy as np import matplotlib.pyplot as plt Series Series 是 ...

  5. pandas DataFrame 数据处理常用操作

    Xgboost调参: https://wuhuhu800.github.io/2018/02/28/XGboost_param_share/ https://blog.csdn.net/hx2017/ ...

  6. Python时间处理,datetime中的strftime/strptime+pandas.DataFrame.pivot_table(像groupby之类 的操作)

    python中datetime模块非常好用,提供了日期格式和字符串格式相互转化的函数strftime/strptime 1.由日期格式转化为字符串格式的函数为: datetime.datetime.s ...

  7. pandas.DataFrame的pivot()和unstack()实现行转列

    示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...

  8. pandas数据操作

    pandas数据操作 字符串方法 Series对象在其str属性中配备了一组字符串处理方法,可以很容易的应用到数组中的每个元素 t = pd.Series(['a_b_c_d','c_d_e',np. ...

  9. 如何迭代pandas dataframe的行

    from:https://blog.csdn.net/tanzuozhev/article/details/76713387 How to iterate over rows in a DataFra ...

随机推荐

  1. 非常全的Linux基础知识点

    Linux是每个后端程序员必须要掌握的系统,今天小编就给你分享一篇Linux基础知识点大全,看看你知道多少? 一. 从认识操作系统开始 1.1 操作系统简介 我通过以下四点介绍什么操作系统: 操作系统 ...

  2. Java + selenium window()接口方法介绍

    在浏览器启动的代码中,有一段关于window接口的调用,这篇文章就是来解释介绍这个接口的.代码如下 driver.manage().window().maxmize(); window接口主要是用来控 ...

  3. POJ 1410 Intersection (计算几何)

    题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...

  4. 转 LoadRunner错误处理函数

    在脚本的Run-time Settings中,可以设置在脚本运行过程中发生错误的处理方式.进入到Run-time Settings中,切换到Miscellaneous标签页,可以看到Error Han ...

  5. Java开发用H2数据库

    #JPA Configuration:#spring.jpa.database=MySQLspring.datasource.url=jdbc:h2:mem:jpaspring.datasource. ...

  6. HTTP面试题目

    转自:http://m.blog.csdn.net/zhangliangzi/article/details/51336564 1.说一下什么是Http协议? 对器客户端和 服务器端之间数据传输的格式 ...

  7. vue-router(路由)详细教程

    vue-router(路由)详细教程:https://blog.csdn.net/wulala_hei/article/details/80488727 vue路由组件传参-页面通信:https:// ...

  8. BZOJ 2122 [分块+单调栈+二分](有详解)

    题面 传送门 给定序列d和lim.假设有一个初始价值\(x_0\),则经历第i天后价值变为\(min(x_0+d[i],lim[i])\),记\(f(i,j,x_0)\)表示以初始代价x0依次经过第i ...

  9. Java8 新增BASE64加解密API

    什么是Base64编码? Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法 基于64个字符A-Z,a-z,0-9,+,/ ...

  10. 三步完成Source Insight 4.0 破解安装(转)

    转自:https://blog.csdn.net/biubiuibiu/article/details/78044232 三步完成Source Insight 4.0 破解安装   下载地址有更新,之 ...