Pandas中DataFrame交换列顺序的方法实现
作者:请叫我算术嘉 时间:2023-01-21 10:21:08
一、获取DataFrame列标签
import pandas as pd
file_path = '/Users/Arithmetic/da-rnn-master/data/collectd67_power_after_test_smooth.csv'
dataset = pd.read_csv(file_path)
cols = list(dataset)
['ps_state-stopped', 'ps_state-running', 'ps_state-blocked', 'ps_state-paging', 'ps_state-sleeping', 'ps_state-zombies', 'fork_rate', 'cpu-2-system', 'cpu-2-nice', 'cpu-2-steal',...]
二、改变列标签为指定顺序
import pandas as pd
file_path = '/Users/Arithmetic/da-rnn-master/data/collectd67_power_after_test_smooth.csv'
dataset = pd.read_csv(file_path)
cols = list(dataset)
print(cols)
cols.insert(0, cols.pop(cols.index('ps_state-running')))
print(cols)
这里改变第一列和第二列的位置顺序,用到了python list中的两个方法
insert方法:
1.功能
insert()函数用于将指定对象插入列表的指定位置。
2.语法
list.insert(index, obj)
3.参数
index: 对象obj需要插入的索引位置。
obj: 插入列表中的对象。
pop() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
三、利用loc获取新的DataFrame,拷贝交换顺序后的DataFrame
import pandas as pd
file_path = '/Users/Arithmetic/da-rnn-master/data/collectd67_power_after_test_smooth.csv'
dataset = pd.read_csv(file_path)
cols = list(dataset)
print(cols)
cols.insert(0, cols.pop(cols.index('ps_state-running')))
print(cols)
data = dataset.loc[:, cols]
四、保存csv覆盖原来的csv
import pandas as pd
file_path = '/Users/Arithmetic/da-rnn-master/data/collectd67_power_after_test_smooth.csv'
dataset = pd.read_csv(file_path)
cols = list(dataset)
print(cols)
cols.insert(0, cols.pop(cols.index('ps_state-running')))
print(cols)
data = dataset.loc[:, cols]
data.to_csv(file_path, index=False)
五、也可以这样
import pandas as pd
file_path = '/Users/Arithmetic/da-rnn-master/data/collectd67_power_after_test_smooth.csv'
dataset = pd.read_csv(file_path)
cols = list(dataset)
print(cols)
cols.insert(0, cols.pop(cols.index('ps_state-running')))
print(cols)
dataset.loc[:, ['ps_state-running', 'ps_state-stopped']] = dataset[['ps_state-stopped', 'ps_state-running']].values
dataset.columns = cols
dataset.to_csv(file_path, index=False)
来源:https://blog.csdn.net/ssjdoudou/article/details/103721118
标签:Pandas,DataFrame,列,顺序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
golang 用msgpack高效序列化的案例
2024-04-26 17:32:52
MySQL左联多表查询where条件写法示例
2024-01-14 08:44:43
![](https://img.aspxhome.com/file/2023/1/109831_0s.jpg)
Python实现新版正方系统滑动验证码识别
2022-11-08 09:14:32
![](https://img.aspxhome.com/file/2023/5/77705_0s.gif)
详解MySQL分组链接的使用技巧
2024-01-14 03:40:37
![](https://img.aspxhome.com/file/2023/2/107412_0s.png)
python实习总结(yeild,async,azwait和协程)
2021-06-16 20:40:45
idea连接sql sever2019图文教程(超详细)
2024-01-15 03:03:13
![](https://img.aspxhome.com/file/2023/9/104109_0s.png)
Golang两行代码实现发送钉钉机器人消息
2024-04-26 17:30:39
![](https://img.aspxhome.com/file/2023/3/134023_0s.png)
巧用Javascript的逻辑运算符
2024-04-16 09:47:42
centos下安装mysql服务器的方法
2024-01-21 06:51:54
Python中列表遍历使用range和enumerate的区别讲解
2023-10-03 12:34:18
PHP lcfirst()函数定义与用法
2023-06-05 01:25:20
![](https://img.aspxhome.com/file/2023/9/55379_0s.png)
PHP中使用正则表达式提取中文实现笔记
2023-09-08 18:35:21
![](https://img.aspxhome.com/file/2023/3/55513_0s.png)
Python开发桌面小程序功能
2023-07-01 14:46:59
![](https://img.aspxhome.com/file/2023/3/110463_0s.png)
如何利用Pyecharts可视化微信好友
2022-04-13 07:34:14
![](https://img.aspxhome.com/file/2023/0/86450_0s.gif)
MSSQL 2005 LOG备份webshell的方法
2024-01-16 16:43:24
go语言reflect.Type 和 reflect.Value 应用示例详解
2023-07-18 11:28:43
MySQL索引操作命令详解
2024-01-29 01:57:55
Python标准库之typing的用法(类型标注)
2021-09-27 01:25:24
![](https://img.aspxhome.com/file/2023/0/65100_0s.png)
讲解MySQL数据库字符集出错的解决方法
2008-12-02 14:32:00
详解golang碎片整理之 fmt.Scan
2024-02-20 12:51:16