import pandas as pd
import os
os.chdir(r'C:\Users\Hans\Desktop\data_analysis\test_data')
df = pd.read_csv('ChinaBank.csv')
df.head()
|
Unnamed: 0 |
Date |
Open |
High |
Low |
Close |
Volume |
0 |
1 |
2014-01-02 |
2.62 |
2.62 |
2.59 |
2.61 |
41632500 |
1 |
2 |
2014-01-03 |
2.60 |
2.61 |
2.56 |
2.56 |
45517700 |
2 |
3 |
2014-01-06 |
2.57 |
2.57 |
2.50 |
2.53 |
68674700 |
3 |
4 |
2014-01-07 |
2.51 |
2.52 |
2.49 |
2.52 |
53293800 |
4 |
5 |
2014-01-08 |
2.51 |
2.54 |
2.49 |
2.51 |
69087900 |
new_columns = df.columns.to_list()
new_columns
['Unnamed: 0', 'Date', 'Open', 'High', 'Low', 'Close', 'Volume']
n = 2
for i in ['Close', 'Volume']:
new_columns.insert(n,i)
n+=1
new_columns
['Unnamed: 0',
'Date',
'Close',
'Volume',
'Open',
'High',
'Low',
'Close',
'Volume']
new_columns.pop(-1)
new_columns
['Unnamed: 0', 'Date', 'Close', 'Volume', 'Open', 'High', 'Low', 'Close']
new_columns.pop(-1)
new_columns
['Unnamed: 0', 'Date', 'Close', 'Volume', 'Open', 'High', 'Low']
df02 = df.reindex(columns=new_columns)
df02.head()
|
Unnamed: 0 |
Date |
Close |
Volume |
Open |
High |
Low |
0 |
1 |
2014-01-02 |
2.61 |
41632500 |
2.62 |
2.62 |
2.59 |
1 |
2 |
2014-01-03 |
2.56 |
45517700 |
2.60 |
2.61 |
2.56 |
2 |
3 |
2014-01-06 |
2.53 |
68674700 |
2.57 |
2.57 |
2.50 |
3 |
4 |
2014-01-07 |
2.52 |
53293800 |
2.51 |
2.52 |
2.49 |
4 |
5 |
2014-01-08 |
2.51 |
69087900 |
2.51 |
2.54 |
2.49 |