Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ValueError: cannot reindex from a duplicate axis
- import pandas as pd
- df = pd.read_csv('data.csv', header=None, names = ['A','B','C'])
- print(df)
- A B C
- 0 1 2 3
- 1 4 5 6
- 2 7 8 9
- 3 10 11 12
- df['D'] = pd.np.nan # this creates an empty series
- # and appends to the right
- print(df)
- A B C D
- 0 1 2 3 NaN
- 1 4 5 6 NaN
- 2 7 8 9 NaN
- 3 10 11 12 NaN
- df = df[['D','A','B','C']] # rearrange as you like
- print(df)
- D A B C
- 0 NaN 1 2 3
- 1 NaN 4 5 6
- 2 NaN 7 8 9
- 3 NaN 10 11 12
- df['new'] = df.index
- colnames = df.columns.tolist()
- colnames = colnames[-1:] + colnames[:-1]
- df = df[colnames]
- df = YourDataFrame
- col = "Your Column You Want To Move To The Start Of YourDataFrame"
- df = pd.concat([df[col],df.drop(col,axis=1)], axis=1)
Add Comment
Please, Sign In to add comment