SHARE
TWEET

Untitled

a guest Oct 10th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. import glob, os
  3.  
  4.  
  5.  
  6. pd.set_option('display.max_colwidth', -1)
  7.  
  8. def rename(dir, pathAndFilename, pattern, titlePattern):
  9.     os.rename(pathAndFilename, os.path.join(dir, titlePattern))
  10.  
  11. # search for csv files in the working folder    
  12. path = os.path.expanduser("*.csv")
  13.  
  14. # iterate and rename them one by one with the number of the iteration
  15. for i, fname in enumerate(glob.glob(path)):
  16.     rename(os.path.expanduser(''), fname, r'*.csv', r'test{}.csv'.format(i))
  17.  
  18.  
  19. # change separator for CSV file
  20. df1 = pd.read_csv('~/Projects/MYP/Datasets/test/test0.csv', sep="@")
  21. df2 = pd.read_csv('~/Projects/MYP/Datasets/test/test1.csv', sep="@")
  22. df3 = pd.read_csv('~/Projects/MYP/Datasets/test/test29.csv', sep="@")
  23. df4 = pd.read_csv('~/Projects/MYP/Datasets/test/test3.csv', sep="@")
  24. df5 = pd.read_csv('~/Projects/MYP/Datasets/test/test4.csv', sep="@")
  25. df6 = pd.read_csv('~/Projects/MYP/Datasets/test/test5.csv', sep="@")
  26. df7 = pd.read_csv('~/Projects/MYP/Datasets/test/test6.csv', sep="@")
  27. df8 = pd.read_csv('~/Projects/MYP/Datasets/test/test7.csv', sep="@")
  28. df9 = pd.read_csv('~/Projects/MYP/Datasets/test/test8.csv', sep="@")
  29. df10 = pd.read_csv('~/Projects/MYP/Datasets/test/test9.csv', sep="@")
  30. df11 = pd.read_csv('~/Projects/MYP/Datasets/test/test28.csv', sep="@")
  31. df12 = pd.read_csv('~/Projects/MYP/Datasets/test/test11.csv', sep="@")
  32. df13 = pd.read_csv('~/Projects/MYP/Datasets/test/test12.csv', sep="@")
  33. df14 = pd.read_csv('~/Projects/MYP/Datasets/test/test13.csv', sep="@")
  34. df15 = pd.read_csv('~/Projects/MYP/Datasets/test/test14.csv', sep="@")
  35. df16 = pd.read_csv('~/Projects/MYP/Datasets/test/test15.csv', sep="@")
  36. df17 = pd.read_csv('~/Projects/MYP/Datasets/test/test16.csv', sep="@")
  37. df18 = pd.read_csv('~/Projects/MYP/Datasets/test/test17.csv', sep="@")
  38. df19 = pd.read_csv('~/Projects/MYP/Datasets/test/test18.csv', sep="@")
  39. df20 = pd.read_csv('~/Projects/MYP/Datasets/test/test19.csv', sep="@")
  40. df21 = pd.read_csv('~/Projects/MYP/Datasets/test/test20.csv', sep="@")
  41. df22 = pd.read_csv('~/Projects/MYP/Datasets/test/test21.csv', sep="@")
  42. df23 = pd.read_csv('~/Projects/MYP/Datasets/test/test22.csv', sep="@")
  43. df24 = pd.read_csv('~/Projects/MYP/Datasets/test/test23.csv', sep="@")
  44. df25 = pd.read_csv('~/Projects/MYP/Datasets/test/test24.csv', sep="@")
  45. df26 = pd.read_csv('~/Projects/MYP/Datasets/test/test25.csv', sep="@")
  46. df27 = pd.read_csv('~/Projects/MYP/Datasets/test/test26.csv', sep="@")
  47. df28 = pd.read_csv('~/Projects/MYP/Datasets/test/test27.csv', sep="@")
  48.  
  49. #frames = [df1, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12, df13, df14, df15, df16, df17, df18, df19, df20, df21, df22, df23, df24, df25, df26, df27, df28]
  50.  
  51. ###concatenate multiple data CSV files
  52. ##all = pd.concat(frames)
  53. ##
  54. ##print(df1.shape)
  55. ##print(df2.shape)
  56. ##print(all.shape)
  57.  
  58.  
  59. #Dynamically Load multiple csv file into Dataframe
  60.  
  61. result = pd.DataFrame()
  62.  
  63. path = os.path.expanduser("*.csv")
  64.  
  65. for fname in glob.glob(path):
  66.     head, tail = os.path.split(fname)
  67.     df = pd.read_csv(fname, sep="@")
  68.     df2 = df.sort_values(by=['Views'], ascending=False).drop(['Favorite', 'videoID'], axis=1).iloc[15:20,:]
  69.     df2['channel'] = tail
  70.     result = pd.concat([result, df2])
  71. result.sort_values(by=['channel']).iloc[0:10,]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top