Realization of DataFrame Exchange Column Sequence Method in Pandas
- 2021-08-28 20:31:32
- OfStack
1. Get the DataFrame column label
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',...]
2. Change the column labels to the specified order
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)
Here, changing the position order of the first column and the second column uses two methods in python list
insert method:
1. Functions
The insert () function is used to insert the specified object into the list at the specified location.
2. Grammar
list.insert(index, obj)
3. Parameters
index: The index location where the object obj needs to be inserted.
obj: Inserts an object in the list.
The pop () function removes one element from the list (the last by default) and returns the value of that element
3. Using loc to obtain new DataFrame, and copying DataFrame after exchange sequence
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]
4. Save csv to overwrite the original 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)
5. You can do the same
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)