Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. Below is your python code :
  2.  
  3. import pandas as pd
  4.  
  5. def readmyfile():
  6. # reading csv file
  7. result = pd.read_csv('data.csv')
  8. dates = [] # list to store elements in first column
  9. colors = [] # list to store elements in last column
  10. # iterating over first and last column
  11. for i in range(result.shape[0]):
  12. dates.append(result.iloc[i,0])
  13. colors.append(result.iloc[i,-1])
  14. # returning both lists
  15. return dates,colors
  16.  
  17. def writetofile(output_file,dates,colors):
  18. # writing first row title
  19. output_file.write("Dates\tColors\n")
  20. # writing the two lists to the txt file
  21. for i in range(len(dates)):
  22. line = str(dates[i])+"\t"+str(colors[i])+"\n"
  23. output_file.write(line)
  24.  
  25. if __name__ == '__main__':
  26. dates,colors = readmyfile()
  27. # opening the txt file
  28. file1 = open("Dates_Colors.txt","w")
  29. writetofile(file1,dates,colors)
  30. file1.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement