(编辑:jimmy 日期: 2024/12/28 浏览:2)
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() 函数用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值
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]
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)