Advertisement
makispaiktis

ML - Lab 1 - Basics in DataFrame

Oct 18th, 2022 (edited)
1,042
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. from random import randrange
  2. import pandas as pd
  3.  
  4. # Create a DataFrame
  5. nums = [1, 4, 9, 16, 25, 36, 49]
  6. strs = ["aa", "bb", "cc", "dd", "ee", "ff", "gg"]
  7. bools = [True, False, True, True, False, False, True]
  8. df = pd.DataFrame(list(zip(nums, strs, bools)), columns=['ns', 'ss', 'bs'])
  9. print("**********************************")
  10. print(df)
  11. print()
  12. print(df.ns)
  13. print()
  14. print(df['ss'])
  15. print()
  16.  
  17. print("**********************************")
  18. # Rows
  19. row_ind = 0
  20. row = df.loc[row_ind]
  21. print(str(row) + ", length = " + str(len(row)))
  22. print()
  23. # Columns
  24. col_name = 'bs'
  25. col = df.loc[:, col_name]
  26. print(str(col) + ", length = " + str(len(col)))
  27. print()
  28. # Elements
  29. element = df.loc[1, "bs"]
  30. print(element)
  31. print()
  32. # Columns with condition True
  33. rows_ind = df.loc[df.bs]
  34. print(rows_ind)
  35. print()
  36. print()
  37.  
  38. # Load a file .csv
  39. print("**********************************")
  40. people = pd.read_csv("people.txt")
  41. print(people)
  42. print()
  43. people = pd.read_csv("people2.txt", sep=";", header=None, names=["Age", "Height", "Weight"])
  44. print(people)
  45. print()
  46. # Missing values are found by average
  47. people = people.fillna(people.mean())
  48. print(people)
  49. print()
  50. print()
  51.  
  52. # Statistical info
  53. print("**********************************")
  54. print("Head: ")
  55. print(people.head())
  56. print()
  57. print("Describe: ")
  58. print(people.describe())
  59. print()
  60. print("Null elements in each column: ")
  61. print(people.isnull().sum())
  62. print()
  63. print()
  64.  
  65. # Convert categorial data to numerical
  66. from sklearn.preprocessing import OneHotEncoder
  67. # Create Encoder
  68. encoder = OneHotEncoder(handle_unknown="ignore", sparse=False)
  69. encoder.fit(people)
  70. people_one_hot = encoder.transform(people)
  71. print("**********************************")
  72. print(people_one_hot)
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement