這篇文章主要介紹怎么更改pandas dataframe中兩列的位置,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)建站從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元都蘭做網(wǎng)站,已為上家服務(wù),為都蘭各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792更改 pandas dataframe 中兩列的位置:
把其中的某列移到第一列的位置。
原來的 df 是:
df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01.csv') Net Upper Lower Mid Zsore Answer option More than once a day 0% 0.22% -0.12% 2 65 Once a day 0% 0.32% -0.19% 3 45 Several times a week 2% 2.45% 1.10% 4 78 Once a week 1% 1.63% -0.40% 6 65
要將 Mid 這一列移動到第一列?
Mid Upper Lower Net Zsore Answer option More than once a day 2 0.22% -0.12% 0% 65 Once a day 3 0.32% -0.19% 0% 45 Several times a week 4 2.45% 1.10% 2% 78 Once a week 6 1.63% -0.40% 1% 65
解決辦法:(使用 ix )
法一:
In [27]: # get a list of columns cols = list(df) # move the column to head of list using index, pop and insert cols.insert(0, cols.pop(cols.index('Mid'))) cols Out[27]: ['Mid', 'Net', 'Upper', 'Lower', 'Zsore'] In [28]: # use ix to reorder df = df.ix[:, cols] df Out[28]: Mid Net Upper Lower Zsore Answer_option More_than_once_a_day 2 0% 0.22% -0.12% 65 Once_a_day 3 0% 0.32% -0.19% 45 Several_times_a_week 4 2% 2.45% 1.10% 78 Once_a_week 6 1% 1.63% -0.40% 65
法二:
In [39]: mid = df['Mid'] df.drop(labels=['Mid'], axis=1,inplace = True) df.insert(0, 'Mid', mid) df Out[39]: Mid Net Upper Lower Zsore Answer_option More_than_once_a_day 2 0% 0.22% -0.12% 65 Once_a_day 3 0% 0.32% -0.19% 45 Several_times_a_week 4 2% 2.45% 1.10% 78 Once_a_week 6 1% 1.63% -0.40% 65
#### full data df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01.csv') def func(x): return str(x['time_stamp'])+str(x['user_id']) df['session_id'] = df.apply(func, axis=1) del df['time_stamp'] sessionID=df['session_id'] df.drop(labels=['session_id'],axis=1,inplace=True) df.insert(0,'session_id',sessionID) df.to_csv('I:/Papers/consumer/codeandpaper/TmallData/result02.csv')
最終的處理結(jié)果:
以上是“怎么更改pandas dataframe中兩列的位置”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前名稱:怎么更改pandasdataframe中兩列的位置-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article30/cddppo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、App設(shè)計、微信小程序、網(wǎng)頁設(shè)計公司、Google、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容