Guest User

Untitled

a guest
Jun 19th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. ValueError: cannot reindex from a duplicate axis
  2.  
  3. import pandas as pd
  4.  
  5. df = pd.read_csv('data.csv', header=None, names = ['A','B','C'])
  6.  
  7. print(df)
  8.  
  9. A B C
  10. 0 1 2 3
  11. 1 4 5 6
  12. 2 7 8 9
  13. 3 10 11 12
  14.  
  15. df['D'] = pd.np.nan # this creates an empty series
  16. # and appends to the right
  17.  
  18. print(df)
  19.  
  20. A B C D
  21. 0 1 2 3 NaN
  22. 1 4 5 6 NaN
  23. 2 7 8 9 NaN
  24. 3 10 11 12 NaN
  25.  
  26. df = df[['D','A','B','C']] # rearrange as you like
  27.  
  28. print(df)
  29.  
  30. D A B C
  31. 0 NaN 1 2 3
  32. 1 NaN 4 5 6
  33. 2 NaN 7 8 9
  34. 3 NaN 10 11 12
  35.  
  36. df['new'] = df.index
  37.  
  38. colnames = df.columns.tolist()
  39.  
  40. colnames = colnames[-1:] + colnames[:-1]
  41.  
  42. df = df[colnames]
  43.  
  44. df = YourDataFrame
  45. col = "Your Column You Want To Move To The Start Of YourDataFrame"
  46. df = pd.concat([df[col],df.drop(col,axis=1)], axis=1)
Add Comment
Please, Sign In to add comment