Using Series (Row-Wise)

import pandas as pd
purchase_1 = pd.Series({'Name': 'Chris',
'Item Purchased': 'Dog Food',
'Cost': 22.50})
purchase_2 = pd.Series({'Name': 'Kevyn',
'Item Purchased': 'Kitty Litter',
'Cost': 2.50})
purchase_3 = pd.Series({'Name': 'Vinod',
'Item Purchased': 'Bird Seed',
'Cost': 5.00})
df = pd.DataFrame([purchase_1, purchase_2, purchase_3], index=['Store 1', 'Store 1', 'Store 2'])
df.head()

Using Series (Column-Wise)

s1 =pd.Series([.25,.5,.75,1],index = ['a','b','c','d']
s2 =pd.Series([.5,.75,1,.25],index = ['a','b','c','d']
df = pd.DataFrame({’s1’:s1,’s2’:s2})
print (df)

Using Dictionary (Columnwise)

data = {'Fruit':['Apple','Pear','Strawberry'],
'Amount':[3,2,5],
'Price':[10,9,8]}
df = DataFrame(data)
print(df)

Using Nested Dictionary

The outer dictionary is columnwise and the inner dictionary is rowwise.

data = {'Amount':{'Apple':3,'Pear':2,'Strawberry':5},
'Price':{'Apple':10,'Pear':9,'Strawberry':8}}
df = DataFrame(data)
print(df)

[Python Cookbook] Pandas: 3 Ways to define a DataFrame的更多相关文章

  1. [Python Cookbook]Pandas: How to increase columns for DataFrame?Join/Concat

    1. Combine Two Series series1=pd.Series([1,2,3],name='s1') series2=pd.Series([4,5,6],name='s2') df = ...

  2. [Python Cookbook] Pandas Groupby

    Groupby Count # Party’s Frequency of donations nyc.groupby(’Party’)[’contb receipt amt’].count() The ...

  3. [Python Cookbook] Numpy: Multiple Ways to Create an Array

    Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...

  4. [Python Cookbook] Pandas: Indexing of DataFrame

    Selecting a Row df.loc[index] # if index is a string, add ' '; if index is a number, no ' ' or df.il ...

  5. python cookbook学习1

    python cookbook学习笔记 第一章 文本(1) 1.1每次处理一个字符(即每次处理一个字符的方式处理字符串) print list('theString') #方法一,转列表 结果:['t ...

  6. 《Python cookbook》 “定义一个属性可由用户修改的装饰器” 笔记

    看<Python cookbook>的时候,第9.5部分,"定义一个属性可由用户修改的装饰器",有个装饰器理解起来花了一些时间,做个笔记免得二刷这本书的时候忘了 完整代 ...

  7. 量化投资与Python之pandas

    pandas:数据分析 pandas是一个强大的Python数据分析的工具包.pandas是基于NumPy构建的. pandas的主要功能具备对其功能的数据结构DataFrame.Series集成时间 ...

  8. Python利用pandas处理Excel数据的应用

    Python利用pandas处理Excel数据的应用   最近迷上了高效处理数据的pandas,其实这个是用来做数据分析的,如果你是做大数据分析和测试的,那么这个是非常的有用的!!但是其实我们平时在做 ...

  9. python书籍推荐:Python Cookbook第三版中文

    所属网站分类: 资源下载 > python电子书 作者:熊猫烧香 链接:http://www.pythonheidong.com/blog/article/44/ 来源:python黑洞网 内容 ...

随机推荐

  1. Ajax异步与JavaScript的一些初浅认识

    向服务器请求数据的技术 有以下五种常用技术用于向服务器请求数据 XMLHttpRequest(XHR) Dynamic script tag insertion(动态脚本标签插入) iframes C ...

  2. ansible自动安装jdk

    脚本功能:安装jdk 测试环境:CentOS6.7 说明: 1.卸载系统自带的openjdk,重新安装Oracle jdk,支持Hotspot,性能更好,更稳定. 2.jdk软件包按文档说明进行定制( ...

  3. nosetests

    1.nosetests 执行出测试报告 提前安装 插件nose-html系列插件   nosetests -v --with-html-output --html-out-file=报告名.html ...

  4. 遍历列表,打印:我叫name,今年age岁,家住dizhi,电话phone(我是通过下标取键得到对应值,有哪位大神来个更简单的)

    lt = [ {'name':'小王', 'age':18, 'info':[('phone', '123'), ('dizhi', '广州')]}, {'name':'小芳', 'age':19, ...

  5. Windows批处理中获取日期和时间

    编写Windows bat 批处理时经常会需要使用到日期和时间作为文件名,所以是非常重要的. 如何获取日期呢? 格式: %date% 结果: -- 如何获取时间呢? 格式: %time% 结果: :: ...

  6. cloud-init简介及组件说明

    http://cloudinit.readthedocs.io/en/latest/topics/examples.html介绍:    cloud-init是专为云环境中虚拟机的初始化而开发的工具, ...

  7. SqlServer中截取小数位数

    方法一:convert(float,字段名) as 别名 select convert(float,round(10.123232,2)) 结果:10.12 select convert(float, ...

  8. 解决IE浏览器中出现“Resource interpreted as Document but transferred with MIME type application/json”问题

    在上传图片时,使用ajax提交,返回的数据格式为json.在测试时发现IE浏览器中,上传图片后,没有显示图片,而是弹出一个提示:是否保存UploadImg.json文件:而在其他浏览器中正常. 在Ch ...

  9. NOIP2017赛前考试注意事项总结

     考前: 考试前把读入优化和库以及对拍文件打好做好准备工作,另外注意放松心态,太紧张了肯定考不好··将自己的注意力集中起来  考场策略: 考试的基本策略是对每于道题先想个20分钟,如果想不出个靠谱的方 ...

  10. (转)myeclipse工程 junit不能运行 ClassNotFoundException

    博文转自:http://www.cnblogs.com/java-zone/articles/2730722.html myeclipse工程   junit不能运行 1 2 3 4 5 6 7 8 ...