最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pandas.DataFrame的pivot()和unstack()實(shí)現(xiàn)行轉(zhuǎn)列

 更新時(shí)間:2019年07月06日 11:43:23   作者:Leohahah  
這篇文章主要介紹了pandas.DataFrame的pivot()和unstack()實(shí)現(xiàn)行轉(zhuǎn)列,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

示例:有如下表需要進(jìn)行行轉(zhuǎn)列:

代碼如下:

# -*- coding:utf-8 -*-

import pandas as pd

import MySQLdb

from warnings import filterwarnings

# 由于create table if not exists總會(huì)拋出warning,因此使用filterwarnings消除

filterwarnings('ignore', category = MySQLdb.Warning)

from sqlalchemy import create_engine

import sys

if sys.version_info.major<3:

 reload(sys)

 sys.setdefaultencoding("utf-8")

 # 此腳本適用于python2和python3

host,port,user,passwd,db,charset="192.168.1.193",3306,"leo","mysql","test","utf8"

 

def get_df():

 global host,port,user,passwd,db,charset

 conn_config={"host":host, "port":port, "user":user, "passwd":passwd, "db":db,"charset":charset}

 conn = MySQLdb.connect(**conn_config)

 result_df=pd.read_sql('select UserName,Subject,Score from TEST',conn)

 return result_df

 

def pivot(result_df):

 df_pivoted_init=result_df.pivot('UserName','Subject','Score')

 df_pivoted = df_pivoted_init.reset_index() # 將行索引也作為DataFrame值的一部分,以方便存儲(chǔ)數(shù)據(jù)庫(kù)

 return df_pivoted_init,df_pivoted

 # 返回的兩個(gè)DataFrame,一個(gè)是以姓名作index的,一個(gè)是以數(shù)字序列作index,前者用于unpivot,后者用于save_to_mysql

 

def unpivot(df_pivoted_init):

 # unpivot需要進(jìn)行df_pivoted_init二維表格的行、列索引遍歷,需要拼SQL因此不能使用save_to_mysql存數(shù)據(jù),這里使用SQL和MySQLdb接口存

 insert_sql="insert into test_unpivot(UserName,Subject,Score) values "

 # 處理值為NaN的情況

 df_pivoted_init=df_pivoted_init.fillna(0)

 for col in df_pivoted_init.columns:

  for index in df_pivoted_init.index:

   value=df_pivoted_init.at[index,col]

   if value!=0:

    insert_sql=insert_sql+"('%s','%s',%s)" %(index,col,value)+','

 insert_sql = insert_sql.strip(',')

 global host, port, user, passwd, db, charset

 conn_config = {"host": host, "port": port, "user": user, "passwd": passwd, "db": db, "charset": charset}

 conn = MySQLdb.connect(**conn_config)

 cur=conn.cursor()

 cur.execute("create table if not exists test_unpivot like TEST")

 cur.execute(insert_sql)

 conn.commit()

 conn.close()

 

def save_to_mysql(df_pivoted,tablename):

 global host, port, user, passwd, db, charset

 """

 只有使用sqllite時(shí)才能指定con=connection實(shí)例,其他數(shù)據(jù)庫(kù)需要使用sqlalchemy生成engine,engine的定義可以添加?來(lái)設(shè)置字符集和其他屬性

 """

 conn="mysql://%s:%s@%s:%d/%s?charset=%s" %(user,passwd,host,port,db,charset)

 mysql_engine = create_engine(conn)

 df_pivoted.to_sql(name=tablename, con=mysql_engine, if_exists='replace', index=False)

 

# 從TEST表讀取源數(shù)據(jù)至DataFrame結(jié)構(gòu)

result_df=get_df()

# 將源數(shù)據(jù)行轉(zhuǎn)列為二維表格形式

df_pivoted_init,df_pivoted=pivot(result_df)

# 將二維表格形式的數(shù)據(jù)存到新表test中

save_to_mysql(df_pivoted,'test')

# 將被行轉(zhuǎn)列的數(shù)據(jù)unpivot,存入test_unpivot表中

unpivot(df_pivoted_init)

結(jié)果如下:

關(guān)于Pandas DataFrame類自帶的pivot方法:

DataFrame.pivot(index=None, columns=None, values=None):

Return reshaped DataFrame organized by given index / column values.

這里只有3個(gè)參數(shù),是因?yàn)閜ivot之后的結(jié)果一定是二維表格,只需要行列及其對(duì)應(yīng)的值,而且也因?yàn)槭嵌S表格,unpivot之后is_pass列是肯定會(huì)丟失的,因此一開(kāi)始我就沒(méi)查這個(gè)列。

補(bǔ)充說(shuō)明:

在學(xué)習(xí)到Pandas的層次化索引部分時(shí)發(fā)現(xiàn)了2個(gè)很有意思的函數(shù),也可以進(jìn)行行列互轉(zhuǎn),其用法如下:(很久之后我才意識(shí)到,pivot只是封裝了unstack的一個(gè)快捷方式而已,其本質(zhì)上還是先用set_index建立層次化索引,然后用unstack進(jìn)行重塑,就像我在下面示例做的操作)

# -*- coding:utf-8 -*-

import pandas as pd

import MySQLdb

from warnings import filterwarnings

# 由于create table if not exists總會(huì)拋出warning,因此使用filterwarnings消除

filterwarnings('ignore', category = MySQLdb.Warning)

from sqlalchemy import create_engine

import sys

if sys.version_info.major<3:

 reload(sys)

 sys.setdefaultencoding("utf-8")

 # 此腳本適用于python2和python3

host,port,user,passwd,db,charset="192.168.1.193",3306,"leo","mysql","test","utf8"

 

def get_df():

 global host,port,user,passwd,db,charset

 conn_config={"host":host, "port":port, "user":user, "passwd":passwd, "db":db,"charset":charset}

 conn = MySQLdb.connect(**conn_config)

 result_df=pd.read_sql('select UserName,Subject,Score from TEST',conn)

 return result_df

 

def pivot(result_df):

 df_pivoted_init=result_df.pivot('UserName','Subject','Score')

 df_pivoted = df_pivoted_init.reset_index() # 將行索引也作為DataFrame值的一部分,以方便存儲(chǔ)數(shù)據(jù)庫(kù)

 return df_pivoted_init,df_pivoted

 # 返回的兩個(gè)DataFrame,一個(gè)是以姓名作index的,一個(gè)是以數(shù)字序列作index,前者用于unpivot,后者用于save_to_mysql

 

def unpivot(df_pivoted_init):

 # unpivot需要進(jìn)行df_pivoted_init二維表格的行、列索引遍歷,需要拼SQL因此不能使用save_to_mysql存數(shù)據(jù),這里使用SQL和MySQLdb接口存

 insert_sql="insert into test_unpivot(UserName,Subject,Score) values "

 # 處理值為NaN的情況

 df_pivoted_init=df_pivoted_init.fillna(0)

 for col in df_pivoted_init.columns:

  for index in df_pivoted_init.index:

   value=df_pivoted_init.at[index,col]

   if value!=0:

    insert_sql=insert_sql+"('%s','%s',%s)" %(index,col,value)+','

 insert_sql = insert_sql.strip(',')

 global host, port, user, passwd, db, charset

 conn_config = {"host": host, "port": port, "user": user, "passwd": passwd, "db": db, "charset": charset}

 conn = MySQLdb.connect(**conn_config)

 cur=conn.cursor()

 cur.execute("create table if not exists test_unpivot like TEST")

 cur.execute(insert_sql)

 conn.commit()

 conn.close()

 

def save_to_mysql(df_pivoted,tablename):

 global host, port, user, passwd, db, charset

 """

 只有使用sqllite時(shí)才能指定con=connection實(shí)例,其他數(shù)據(jù)庫(kù)需要使用sqlalchemy生成engine,engine的定義可以添加?來(lái)設(shè)置字符集和其他屬性

 """

 conn="mysql://%s:%s@%s:%d/%s?charset=%s" %(user,passwd,host,port,db,charset)

 mysql_engine = create_engine(conn)

 df_pivoted.to_sql(name=tablename, con=mysql_engine, if_exists='replace', index=False)

 

# 從TEST表讀取源數(shù)據(jù)至DataFrame結(jié)構(gòu)

result_df=get_df()

# 將源數(shù)據(jù)行轉(zhuǎn)列為二維表格形式

df_pivoted_init,df_pivoted=pivot(result_df)

# 將二維表格形式的數(shù)據(jù)存到新表test中

save_to_mysql(df_pivoted,'test')

# 將被行轉(zhuǎn)列的數(shù)據(jù)unpivot,存入test_unpivot表中

unpivot(df_pivoted_init)

以上利用了Pandas的層次化索引,實(shí)際上這也是層次化索引一個(gè)主要的用途,結(jié)合本例我們可以把代碼改成如下:

result_df=pd.read_sql('select UserName,Subject,Score from TEST',conn)

# 在從數(shù)據(jù)庫(kù)中獲取的數(shù)據(jù)格式是這樣的:

    UserName Subject Score

0    張三   語(yǔ)文  80.0

1    張三   數(shù)學(xué)  90.0

2    張三   英語(yǔ)  70.0

3    張三   生物  85.0

4    李四   語(yǔ)文  80.0

5    李四   數(shù)學(xué)  92.0

6    李四   英語(yǔ)  76.0

7    王五   語(yǔ)文  60.0

8    王五   數(shù)學(xué)  82.0

9    王五   英語(yǔ)  96.0

10    王五   生物  78.0

# 如果要使用層次化索引,那么我們只需要把UserName和Subject列設(shè)置為層次化索引,Score為其對(duì)應(yīng)的值即可,我們借用set_index()函數(shù):

df=result_df.set_index(['UserName','Subject'])

In [112]: df.unstack()

Out[112]: 

     Score         

Subject   數(shù)學(xué)  生物  英語(yǔ)  語(yǔ)文

UserName            

張三    90.0 85.0 70.0 80.0

李四    92.0  NaN 76.0 80.0

王五    82.0 78.0 96.0 60.0

# 使用stack可以將unstack的結(jié)果轉(zhuǎn)回來(lái),這樣就也在形式上實(shí)現(xiàn)了行列互轉(zhuǎn),之后的操作基本一致了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)Excel表格轉(zhuǎn)置與翻譯工具

    Python實(shí)現(xiàn)Excel表格轉(zhuǎn)置與翻譯工具

    本文主要介紹如何使用Python編寫(xiě)一個(gè)GUI程序,能夠讀取Excel文件,將第一個(gè)列的數(shù)據(jù)轉(zhuǎn)置,并將英文內(nèi)容翻譯成中文,有需要的小伙伴可以參考一下
    2024-10-10
  • python中私有函數(shù)調(diào)用方法解密

    python中私有函數(shù)調(diào)用方法解密

    這篇文章主要介紹了python中私有函數(shù)調(diào)用方法,較為詳細(xì)的分析了Python私有函數(shù)的原理與調(diào)用技巧,需要的朋友可以參考下
    2016-04-04
  • Python Django實(shí)現(xiàn)個(gè)人博客系統(tǒng)的搭建

    Python Django實(shí)現(xiàn)個(gè)人博客系統(tǒng)的搭建

    個(gè)人博客是一個(gè)非常好的平臺(tái),可以讓人們分享自己的知識(shí)和經(jīng)驗(yàn),也可以讓人們交流和互動(dòng)。在這篇文章中,我們將介紹如何使用Python Django框架來(lái)開(kāi)發(fā)一個(gè)個(gè)人博客系統(tǒng),希望對(duì)大家有所幫助
    2023-04-04
  • 使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案

    使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案

    這篇文章主要介紹了使用 Python ssh 遠(yuǎn)程登陸服務(wù)器的最佳方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python文件絕對(duì)路徑寫(xiě)法介紹(windows)

    python文件絕對(duì)路徑寫(xiě)法介紹(windows)

    今天小編就為大家分享一篇python文件絕對(duì)路徑寫(xiě)法介紹(windows),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python super( )函數(shù)用法總結(jié)

    Python super( )函數(shù)用法總結(jié)

    今天給大家?guī)?lái)的知識(shí)是關(guān)于Python的相關(guān)知識(shí),文章圍繞著super( )函數(shù)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Python中GeoJson和bokeh-1的使用講解

    Python中GeoJson和bokeh-1的使用講解

    今天小編就為大家分享一篇關(guān)于Python中GeoJson和bokeh-1的使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • 最新評(píng)論

    达尔| 宜黄县| 湟源县| 巴楚县| 灵丘县| 抚松县| 尉氏县| 神木县| 玉环县| 齐河县| 镇宁| 武汉市| 潞城市| 阳城县| 平利县| 衢州市| 东至县| 镇平县| 拜城县| 安吉县| 抚宁县| 文昌市| 赞皇县| 嘉黎县| 东兰县| 清新县| 上杭县| 木兰县| 和田县| 蒲城县| 皋兰县| 宜川县| 清河县| 武城县| 抚州市| 临邑县| 肥东县| 石首市| 安远县| 本溪| 萝北县|