python行转列_pandas.DataFrame中pivot()如何实现行转列的问题(代码)
本篇文章給大家?guī)?lái)的內(nèi)容是關(guān)于pandas.DataFrame中pivot()如何實(shí)現(xiàn)行轉(zhuǎn)列的問(wèn)題(代碼),有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。
示例:
有如下表需要進(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.add(0,fill_value=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è)列。
總結(jié)
以上是生活随笔為你收集整理的python行转列_pandas.DataFrame中pivot()如何实现行转列的问题(代码)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: abaqus的python安装文件在哪_
- 下一篇: python openstack rab