更改 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')

最终的处理结果:

更改 pandas dataframe 中两列的位置的更多相关文章

  1. pandas.DataFrame 中的insert(), pop()

    pandas.DataFrame 中的insert(), pop() 在pandas中,del.drop和pop方法都可以用来删除数据,insert可以在指定位置插入数据. 可以看看以下示例. imp ...

  2. pandas DataFrame行或列的删除方法

    pandas DataFrame的增删查改总结系列文章: pandas DaFrame的创建方法 pandas DataFrame的查询方法 pandas DataFrame行或列的删除方法 pand ...

  3. mysql互换表中两列数据

    在开发过程中,有时由于业务等需要把一个表中的两列数据进行交换. 解决方案 使用update命令,这完全得益于MySQL SQL命令功能的强大支持. 表格中原来数据类似如下: select * from ...

  4. [译]如何根据条件从pandas DataFrame中删除不需要的行?

    问题来源:https://stackoverflow.com/questions/13851535/how-to-delete-rows-from-a-pandas-dataframe-based-o ...

  5. 更改pandas dataframe 列的顺序

    摘自 stackoverflow 这是我的df: Net Upper Lower Mid Zsore Answer option More than once a day 0% 0.22% -0.12 ...

  6. pandas | DataFrame中的排序与汇总方法

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是pandas数据处理专题的第六篇文章,我们来聊聊DataFrame的排序与汇总运算. 在上一篇文章当中我们主要介绍了DataFrame ...

  7. pandas.DataFrame对行和列求和及添加新行和列

    导入模块: from pandas import DataFrame import pandas as pd import numpy as np 生成DataFrame数据 df = DataFra ...

  8. pandas.DataFrame 中save方法

    In [5]: frame.save('frame_pickle') ----------------------------------------------------------------- ...

  9. [译]如何将dataframe的两列结合起来?

    我用pandas生成了一个20 x 4000的dataframe.其中两列名为Year和quarter.我想创建一个名为period的变量,将Year = 2000和quarter = q2变为200 ...

随机推荐

  1. 一文学会最常见的10种NLP处理技术

    一文学会最常见的10种NLP处理技术(附资源&代码)   技术小能手 2017-11-21 11:08:29 浏览2562 评论0 算法 HTTPS 序列 自然语言处理 神经网络 摘要: 自然 ...

  2. 10 Big Data Possibilities for 2017 Based on Oracle's Predictions

    2017 will see a host of informed predictions, lower costs, and even business-centric gains, courtesy ...

  3. python限制进程、子进程占用内存大小、CPU时间的方法:resource模块

    内置模块:resource 在mac环境下功能会存在问题.linux下可以使用:但是for i in range(10000)的值必须是10000或者更大的数值才有用.没有搞清楚为什么 #/usr/b ...

  4. SQL基础(二):SQL命令

    1.SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目.SELECT TOP 子句对于拥有数千条记录的大型表来说,是非常有用的(或者比如选取某个最新的数据:我们可 ...

  5. C++11中万能的可调用类型声明std::function<...>

    在C++11中,callable object 包括传统C函数,C++成员函数,函数对象(实现了()运算符的类的实例),lambda表达式(特殊函数对象)共4种.程序设计,特别是程序库设计时,经常需要 ...

  6. Oracle服务启动顺序导致ORA-12514

    在window 上装了oracle11g,按照常规步骤安装完成后一切OK,如下图所示 C:\Users\Administrator>sqlplus /nolog SQL*Plus: Releas ...

  7. (转)unity3d中脚本生命周期(MonoBehaviour lifecycle)

    自:http://blog.csdn.net/qitian67/article/details/18516503 最近在做一个小示例,发现类继承于MonoBehaviour的类,有很多个方法,于是乎必 ...

  8. OpenNMS编译,打包并在Windows下启动

    1.Download Opennms latest source code 2.Download latest Java JDK and install it. Set JAVA_HOME path ...

  9. 翻译记忆软件-塔多思TRADOS经典教程_1

    窗体顶端 网络上关于TRADOS使用的文章很多,但大多数内容涉及的内容都过于广泛,刚准备上手的同行们看了总会觉得不知所云,无从下手.笔者自己也在自学阶段遇到不少麻烦.经过长期的使用和琢磨,终于写了短文 ...

  10. Handler具体解释系列(七)——Activity.runOnUiThread()方法具体解释

    MainActivity例如以下: package cc.testui3; import android.os.Bundle; import android.view.View; import and ...