Python pandas DataFrame操作的實(shí)現(xiàn)代碼
1. 從字典創(chuàng)建Dataframe
>>> import pandas as pd
>>> dict1 = {'col1':[1,2,5,7],'col2':['a','b','c','d']}
>>> df = pd.DataFrame(dict1)
>>> df
col1 col2
0 1 a
1 2 b
2 5 c
3 7 d
2. 從列表創(chuàng)建Dataframe (先把列表轉(zhuǎn)化為字典,再把字典轉(zhuǎn)化為DataFrame)
>>> lista = [1,2,5,7]
>>> listb = ['a','b','c','d']
>>> df = pd.DataFrame({'col1':lista,'col2':listb})
>>> df
col1 col2
0 1 a
1 2 b
2 5 c
3 7 d
3. 從列表創(chuàng)建DataFrame,指定data和columns
>>> a = ['001','zhangsan','M'] >>> b = ['002','lisi','F'] >>> c = ['003','wangwu','M'] >>> df = pandas.DataFrame(data=[a,b,c],columns=['id','name','sex']) >>> df id name sex 0 001 zhangsan M 1 002 lisi F 2 003 wangwu M
4. 修改列名,從['id','name','sex']修改為['Id','Name','Sex']
>>> df.columns = ['Id','Name','Sex'] >>> df Id Name Sex 0 001 zhangsan M 1 002 lisi F 2 003 wangwu M
5. 調(diào)整DataFrame列順序、調(diào)整列編號從1開始
http://www.dhdzp.com/article/163644.htm
6. DataFrame隨機(jī)生成10行4列int型數(shù)據(jù)
>>> import pandas
>>> import numpy
>>> df = pandas.DataFrame(numpy.random.randint(0,100,size=(10, 4)), columns=list('ABCD')) # 0,100指定隨機(jī)數(shù)為0到100之間(包括0,不包括100),size = (10,4)指定數(shù)據(jù)為10行4列,column指定列名
>>> df
A B C D
0 67 28 37 66
1 21 27 43 37
2 73 54 98 85
3 40 78 4 93
4 99 60 63 16
5 48 46 24 61
6 59 52 62 28
7 20 74 36 64
8 14 13 46 60
9 18 44 70 36
7. 用時(shí)間序列做index名
>>> df # 原本index為自動(dòng)生成的0~9
A B C D
0 31 25 45 67
1 62 12 61 88
2 79 36 20 97
3 26 57 50 44
4 24 12 50 1
5 4 61 99 62
6 40 47 52 27
7 83 66 71 4
8 58 59 25 62
9 38 81 60 8
>>> import pandas
>>> dates = pandas.date_range('20180121',periods=10)
>>> dates # 從20180121開始,共10天
DatetimeIndex(['2018-01-21', '2018-01-22', '2018-01-23', '2018-01-24',
'2018-01-25', '2018-01-26', '2018-01-27', '2018-01-28',
'2018-01-29', '2018-01-30'],
dtype='datetime64[ns]', freq='D')
>>> df.index = dates # 將dates賦值給index
>>> df
A B C D
2018-01-21 31 25 45 67
2018-01-22 62 12 61 88
2018-01-23 79 36 20 97
2018-01-24 26 57 50 44
2018-01-25 24 12 50 1
2018-01-26 4 61 99 62
2018-01-27 40 47 52 27
2018-01-28 83 66 71 4
2018-01-29 58 59 25 62
2018-01-30 38 81 60 8
8. dataframe 實(shí)現(xiàn)類SQL操作
pandas官方文檔 Comparison with SQL
https://pandas.pydata.org/pandas-docs/stable/comparison_with_sql.html
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
pyinstaller打包可執(zhí)行文件出現(xiàn)KeyError的問題
這篇文章主要介紹了pyinstaller打包可執(zhí)行文件出現(xiàn)KeyError的問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
python判斷一組數(shù)呈上升還是下降趨勢的操作方法
要判斷一組數(shù)(數(shù)列)是呈上升趨勢、下降趨勢還是無明顯趨勢,我們可以比較數(shù)列中相鄰元素的差值,這篇文章主要介紹了python?如何判斷一組數(shù)呈上升還是下降趨勢,需要的朋友可以參考下2024-06-06
pip版本低導(dǎo)致Python離線包安裝失敗的問題解決
在使用Python進(jìn)行開發(fā)時(shí),安裝各種第三方庫是必不可少的,不過,有時(shí)候我們會(huì)遇到一些麻煩,尤其是當(dāng)pip的版本較低時(shí),下面我們來看看如何解決這一問題吧2025-03-03
keras實(shí)現(xiàn)多GPU或指定GPU的使用介紹
這篇文章主要介紹了keras實(shí)現(xiàn)多GPU或指定GPU的使用介紹,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
Python導(dǎo)入模塊的3種方式超級詳細(xì)講解
這篇文章主要給大家介紹了關(guān)于Python導(dǎo)入模塊的3種方式,本文介紹了在Python中使用模塊的概念和不同的導(dǎo)入方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Python腳本實(shí)現(xiàn)datax全量同步mysql到hive
這篇文章主要和大家分享一下mysql全量同步到hive自動(dòng)生成json文件的python腳本,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參加一下2024-10-10

