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 ...
随机推荐
- ccf 201809-3 元素选择器
一.思路: 1.将结构化文档的每一行处理成一个节点(可定义一个结构体,成员包含标签tag.属性id.层级level.祖先所在行数father). 2.然后整个结构化文档就成了一个树形结构,可从任一节点 ...
- 3403. 题解【NOIP2013模拟】数列变换 (Standard IO)
先看题目: Description 小X 看到堆成山的数列作业十分头疼,希望聪明的你来帮帮他.考虑数列A=[A1,A2,...,An],定义变换f(A,k)=[A2,A3,,,,.Ak,A1,Ak+2 ...
- Windows重置网络命令
我们在日常使用电脑的时候会碰到网络异常,网络故障,想要针对性地去解决是很困难的. 有时候可能查遍了资料,花了大量时间,怎么搞都还是搞不定.所以,这次直接给大家分享一个通过重置网络来解决所有问题的方法. ...
- 为什么学python
一.什么是Python Python [1](英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum发明. ...
- 转 Page Object模式
Page Object模式是Selenium中的一种测试设计模式,主要是将每一个页面设计为一个Class,其中包含页面中需要测试的元素(按钮,输入框,标题 等),这样在Selenium测试页面中可以通 ...
- java面试题-看到那记录到哪
哈希冲突 如果两个不同的元素,通过哈希函数得到的实际存储地址相同怎么办?也就是说,当我们对某个元素进行哈希运算,得到一个存储地址,然后要进行插入的时候,发现已经被其他元素占用了,其实这就是所谓的哈希冲 ...
- elasticsearch Mapping使用自定义分词器
创建索引及配置分析器 PUT /my_index { "settings": { "analysis": { "char_filter": ...
- Ubuntu下使用git clone 的权限问题解决方法
问题1.sign_and_send_pubkey: signing failed: agent refused operation,执行如下语句: eval "$(ssh-agent -s) ...
- shutdown - 关闭系统
总览 SYNOPSIS /sbin/shutdown [-t sec] [-arkhncfF] time [warning-message] 描述 DESCRIPTION shutdown 以一种安全 ...
- 不修改源代码,动态注入Java代码的方法(转)
转自:https://blog.csdn.net/hiphoon_sun/article/details/38707927 有时,我们需要在不修改源代码的前提下往一个第三方的JAVA程序里注入自己的代 ...