Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.nio.file.Files;
  3. import java.nio.file.Paths;
  4. import java.util.ArrayList;
  5.  
  6. public class DataFrame {
  7.  
  8.  
  9. protected ArrayList<ArrayList> list = new ArrayList<ArrayList>();
  10. protected String[] all_names;
  11. protected String[] all_types;
  12.  
  13.  
  14. DataFrame(String[] new_names, String[] new_types){
  15.  
  16. all_names = new_names.clone();
  17. all_types = new_types.clone();
  18. ArrayList al = new ArrayList();
  19.  
  20. for (int i =0; i<new_names.length; ++i){
  21. list.add(new ArrayList());
  22. }
  23. }
  24.  
  25.  
  26.  
  27. public int size(){
  28. return list.get(1).size();
  29. }
  30.  
  31.  
  32. public ArrayList get(String colname){
  33. for(int i = 0; i< all_names.length; i++){
  34. if(colname.equals(all_names[i])){
  35. return list.get(i);
  36. }
  37. }
  38. return new ArrayList();
  39. }
  40.  
  41.  
  42. public DataFrame get(String[] cols, boolean copy){
  43. String[] ntype = new String[cols.length];
  44. for(int i=0; i<cols.length; i++){
  45. for(int j = 0; j< all_names.length; j++){
  46. if(cols[i].equals(all_names[j])){
  47. ntype[i] = all_types[j];
  48. }
  49. }
  50. }
  51. DataFrame dframe = new DataFrame(cols, ntype);
  52.  
  53. for(int i=0; i<cols.length; i++){
  54. for(int j = 0; j< all_names.length; j++){
  55. if(cols[i].equals(all_names[j])){
  56. if(copy) {
  57. dframe.list.add((ArrayList)list.get(j).clone()); // deep copy
  58. } else {
  59. dframe.list.add(list.get(j)); // shallow copy
  60. }
  61. }
  62. }
  63. }
  64. return dframe;
  65. }
  66.  
  67.  
  68. public DataFrame iloc(int i){
  69. DataFrame df = new DataFrame(new String[]{all_names[i]},new String[] {all_types[i]});
  70. df.list.add((ArrayList)list.get(i).clone());
  71. return df;
  72. }
  73.  
  74.  
  75. public DataFrame iloc(int from, int to){
  76.  
  77. String[] n = new String[to-from+1];
  78. String[] t = new String[to-from+1];
  79. int i=0;
  80. for(int it=from; it<=to; it++,i++){
  81. n[i] = all_names[it];
  82. t[i] = all_types[it];
  83. }
  84. DataFrame df = new DataFrame(n,t);
  85. for(int it=from; it<=to; it++){
  86. df.list.add((ArrayList)list.get(it).clone());
  87. }
  88. return df;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement