欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3.5 Pandas模塊之DataFrame用法實例分析

 更新時間:2019年04月23日 11:41:45   作者:loveliuzz  
這篇文章主要介紹了Python3.5 Pandas模塊之DataFrame用法,結(jié)合實例形式詳細分析了Python3.5中Pandas模塊的DataFrame結(jié)構(gòu)創(chuàng)建、讀取、過濾、獲取等相關(guān)操作技巧與注意事項,需要的朋友可以參考下

本文實例講述了Python3.5 Pandas模塊之DataFrame用法。分享給大家供大家參考,具體如下:

1、DataFrame的創(chuàng)建

(1)通過二維數(shù)組方式創(chuàng)建


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#1.DataFrame通過二維數(shù)組創(chuàng)建
print("======DataFrame直接通過二維數(shù)組創(chuàng)建======")
d1 = DataFrame([["a","b","c","d"],[1,2,3,4]])
print(d1)

print("======DataFrame借助array二維數(shù)組創(chuàng)建======")
arr = np.array([
  ["jack",78],
  ["lili",86],
  ["amy",97],
  ["tom",100]
])

d2 = DataFrame(arr,index=["01","02","03","04"],columns=["姓名","成績"])
print(d2)
print("========打印行索引========")
print(d2.index)
print("========打印列索引========")
print(d2.columns)
print("========打印值========")
print(d2.values)

運行結(jié)果:

======DataFrame直接通過二維數(shù)組創(chuàng)建======
   0  1  2  3
0  a  b  c  d
1  1  2  3  4
======DataFrame借助array二維數(shù)組創(chuàng)建======
      姓名   成績
01  jack   78
02  lili   86
03   amy   97
04   tom  100
========打印行索引========
Index(['01', '02', '03', '04'], dtype='object')
========打印列索引========
Index(['姓名', '成績'], dtype='object')
========打印值========
[['jack' '78']
 ['lili' '86']
 ['amy' '97']
 ['tom' '100']]

(2)通過字典方式創(chuàng)建


#2.DataFrame通過字典創(chuàng)建,鍵作為列索引,鍵值作為數(shù)據(jù)值,行索引值自動生成

data = {
  "apart":['1101',"1102","1103","1104"],
  "profit":[2000,4000,5000,3500],
  "month":8
}

d3 = DataFrame(data)
print(d3)
print("========行索引========")
print(d3.index)
print("========列索引========")
print(d3.columns)
print("========數(shù)據(jù)值========")
print(d3.values)

運行結(jié)果:

apart  month  profit
0  1101      8    2000
1  1102      8    4000
2  1103      8    5000
3  1104      8    3500
========行索引========
RangeIndex(start=0, stop=4, step=1)
========列索引========
Index(['apart', 'month', 'profit'], dtype='object')
========數(shù)據(jù)值========
[['1101' 8 2000]
 ['1102' 8 4000]
 ['1103' 8 5000]
 ['1104' 8 3500]]

2、DataFrame數(shù)據(jù)獲取




import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#3.DataFrame獲取數(shù)據(jù)
data = {
  "apart":['1101',"1102","1103","1104"],
  "profit":[2000,4000,5000,3500],
  "month":8
}

d3 = DataFrame(data)
print(d3)

print("======獲取一列數(shù)據(jù)======")
print(d3["apart"])
print("======獲取一行數(shù)據(jù)======")
print(d3.ix[1])

print("======修改數(shù)據(jù)值======")
d3["month"] = [7,8,9,10]        #修改值
d3["year"] = [2001,2001,2003,2004]   #新增列
d3.ix["4"] = np.NaN
print(d3)

運行結(jié)果:

 apart  month  profit
0  1101      8    2000
1  1102      8    4000
2  1103      8    5000
3  1104      8    3500
======獲取一列數(shù)據(jù)======
0    1101
1    1102
2    1103
3    1104
Name: apart, dtype: object
======獲取一行數(shù)據(jù)======
apart     1102
month        8
profit    4000
Name: 1, dtype: object
======修改數(shù)據(jù)值======
  apart  month  profit    year
0  1101    7.0  2000.0  2001.0
1  1102    8.0  4000.0  2001.0
2  1103    9.0  5000.0  2003.0
3  1104   10.0  3500.0  2004.0
4   NaN    NaN     NaN     NaN

3、pandas基本功能


(1)pandas數(shù)據(jù)文件讀取



import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#pandas基本操作
#1.數(shù)據(jù)文件讀取

df = pd.read_csv("data.csv")
print(df)

運行結(jié)果:

    name  age  source
0  gerry   18    98.5
1    tom   21    78.2
2   lili   24    98.5
3   john   20    89.2

(2)數(shù)據(jù)過濾獲取


import numpy as np
import pandas as pd
from pandas import Series,DataFrame

#pandas基本操作
#1.數(shù)據(jù)文件讀取

df = pd.read_csv("data.csv")
print(df)

#2.數(shù)據(jù)過濾獲取

columns = ["姓名","年齡","成績"]
df.columns = columns    #更改列索引
print("=======更改列索引========")
print(df)

#獲取幾列的值
df1 = df[columns[1:]]
print("=======獲取幾列的值========")
print(df1)
print("=======獲取幾行的值========")
print(df.ix[1:3])

#刪除含有NaN值的行
df2 = df1.dropna()
print("=======刪除含有NaN值的行=======")
print(df2)

運行結(jié)果:

 name  age  source
0  gerry   18    98.5
1    tom   21     NaN
2   lili   24    98.5
3   john   20    89.2
=======更改列索引========
      姓名  年齡    成績
0  gerry  18  98.5
1    tom  21   NaN
2   lili  24  98.5
3   john  20  89.2
=======獲取幾列的值========
   年齡    成績
0  18  98.5
1  21   NaN
2  24  98.5
3  20  89.2
=======獲取幾行的值========
     姓名  年齡    成績
1   tom  21   NaN
2  lili  24  98.5
3  john  20  89.2
=======刪除含有NaN值的行=======
   年齡    成績
0  18  98.5
2  24  98.5
3  20  89.2

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • Elasticsearch之倒排索引及索引操作

    Elasticsearch之倒排索引及索引操作

    這篇文章主要為大家介紹了Elasticsearch之倒排索引及索引操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • pytorch:實現(xiàn)簡單的GAN示例(MNIST數(shù)據(jù)集)

    pytorch:實現(xiàn)簡單的GAN示例(MNIST數(shù)據(jù)集)

    今天小編就為大家分享一篇pytorch:實現(xiàn)簡單的GAN示例(MNIST數(shù)據(jù)集),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-01-01
  • Python實現(xiàn)語音識別vosk的示例代碼

    Python實現(xiàn)語音識別vosk的示例代碼

    Vosk是一個功能強大且易于使用的語音識別工具包,它提供了Python綁定,使得在Python中使用Vosk變得非常方便,本文主要介紹了Python實現(xiàn)語音識別vosk的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • python模擬登陸Tom郵箱示例分享

    python模擬登陸Tom郵箱示例分享

    這篇文章主要介紹了python登陸Tom郵箱的示例,大家參考使用吧
    2014-01-01
  • 使用python加密主機文件幾種方法實現(xiàn)

    使用python加密主機文件幾種方法實現(xiàn)

    本文主要介紹了使用python加密主機文件幾種方法實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧
    2023-02-02
  • Python添加進度條tqdm進階使用實例

    Python添加進度條tqdm進階使用實例

    這篇文章主要為大家介紹了Python添加進度條tqdm進階使用實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Python利用matplotlib實現(xiàn)餅圖繪制

    Python利用matplotlib實現(xiàn)餅圖繪制

    Pyplot作為Matplotlib的子庫,提供了和MATLAB差不多的繪圖API。因此Pyplot作為常用的繪圖模塊,能很方便讓用戶繪制2D圖表。本文將為大家介紹如何利用Matplotlib繪制餅圖,感興趣的小伙伴可以了解一下
    2021-12-12
  • Python中斷多重循環(huán)的幾種方法

    Python中斷多重循環(huán)的幾種方法

    跳出單循環(huán)不管是什么編程語言,都有可能會有跳出循環(huán)的需求,本文主要介紹了Python中斷多重循環(huán)的幾種方法,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • 淺談django model的get和filter方法的區(qū)別(必看篇)

    淺談django model的get和filter方法的區(qū)別(必看篇)

    下面小編就為大家?guī)硪黄獪\談django model的get和filter方法的區(qū)別(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • django創(chuàng)建css文件夾的具體方法

    django創(chuàng)建css文件夾的具體方法

    在本文里小編給大家總結(jié)的是關(guān)于django創(chuàng)建css文件夾的具體方法,對此有需要的朋友們參考下吧。
    2020-07-07

最新評論