1

Hello friend i have a simple dataframe that reads the csv file and contains the values of the files but here i'm only reading the selected columns which is choose by the user and appending it with the newly added columns This the program:

final_Array=[]# this array have the values of selected colunms 
data = pd.DataFrame(final_Array)

data['SrID'] = 'SR' + pd.Series(np.arange(1, len(data.index) + 1)).astype(str).str.zfill(4)
tempColumns = ['Year', 'Month']
tempColumns.extend(targetHeader) # "targetHeader" this contains the headers columns names which will added to the other newly added columns
tempColumns.append('SrID')
result = data
result .to_csv('out.csv')

In this i have created a new columns like year,month,srID, which will be appended with older columns which can be selected by the user on UI.. for example(Asset_Id, Asset Family, Asset Name, Keywords etc..) After computation it will show like this:

Year    Month   Asset_Id   Asset Family         Asset Name       SrID
1970    Jan     1          Haptic Analyser      HAL8             SR0001
1970    Jan     2          Hyperdome Inspector  HISP18           SR0002
1970    Feb     5          Superdraft Tunnel    STR46            SR0003
1970    Feb     2          Hyperdome Inspector  HISP4            SR0004
1970    Feb     3          Nano Dial Assembler  NDA18            SR0005
1970    Feb     5          Superdraft Tunnel    STR44            SR0006
1970    Feb     4          Geometric Synthe     GeoSyn29         SR0007
1970    Feb     1          Haptic Analyser      HAL10            SR0008
1970    Feb     5          Superdraft Tunnel    STR41            SR0009
1970    Feb     5          Superdraft Tunnel    STR33            SR0010

This output i will get when i print in the csv file In simple word i just want to print the "SrID" at the first position in csv file so please tell me one efficient way to do this i have tried many way but its not working please help me...thanks

snehil singh
  • 554
  • 1
  • 5
  • 18
  • @coldspeed that is not working i have tried all the possible way which available online please tell me some other way – snehil singh Dec 28 '18 at 12:33
  • 1
    Check out `df.insert(0, ''SrID'', df.pop(''SrID''))` – cs95 Dec 28 '18 at 12:34
  • @coldspeed its not working i don't know but its only chnaged the columns header postions its still printing the same values – snehil singh Dec 28 '18 at 12:52
  • That doesn't happen unless you're doing something wrong. I can reopen this if you can convince me it isn't solving your problem. As of now, I can't see any reason for it not to. This is an identical question to the marked duplicate. – cs95 Dec 28 '18 at 12:53
  • @coldspeed ya i know there are lots of answer online but nobody have applied for UI kind of thing...where user are selecting columns header and output will only generate for that header i'm just tring to append with newly added columns – snehil singh Dec 28 '18 at 12:56
  • @coldspeed please reopen it cause its not working i have tried with tempColumns[-1:] + tempColumns[:-1] this way also we can do but its only changing the columns positions not there values...its really big program so i can not post all the code here – snehil singh Dec 28 '18 at 13:00
  • 1
    `df = df[tempColumns[-1:] + tempColumns[:-1]]` – cs95 Dec 28 '18 at 13:00

0 Answers0