Advertisement
maxim_shlyahtin

df

May 19th, 2023
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. path_right = r"C:\Users\Max-13700kf\PycharmProjects\pythonProject\right_fix.csv"
  5. path_left = r"C:\Users\Max-13700kf\PycharmProjects\pythonProject\left_fix.csv"
  6.  
  7. elagin_right_df = pd.read_csv(path_right) # иногда нужно указать sep
  8. elagin_left_df = pd.read_csv(path_left)
  9. #
  10. # 1
  11. print(elagin_left_df.head())
  12. print('---------------------')
  13. # 2
  14. print(elagin_right_df.isnull().any(), end='\n-------------\n')
  15. print(elagin_right_df.info(), end='\n-------------\n')
  16. print(elagin_left_df.isnull().any(), end='\n-------------\n')
  17. print(elagin_left_df.info(), end='\n-------------\n')
  18.  
  19. # 3
  20. print(elagin_right_df.duplicated(), end='\n-------------\n')
  21. print(elagin_left_df.duplicated(), end='\n-------------\n')
  22.  
  23. #
  24. # #4
  25. elagin_left_df = elagin_left_df.drop_duplicates()
  26. df = pd.merge(elagin_left_df, elagin_right_df, how='outer',
  27.               on=['id', 'male', 'age', 'is_spb', 'married', 'children', 'elagin_attractiv'])
  28. print(df.info())
  29.  
  30. # 5
  31. df = df.sort_values(['education', 'age'], ascending=[True, True])
  32. print(df.head(20))
  33.  
  34. # 6
  35. group = df.groupby(['elagin_attractiv'])
  36. print(group.groups)
  37. for i in group.groups.keys():
  38.     print(f'{i}:{len(group.groups[i])}')
  39. print('-------')
  40.  
  41. # 7
  42. df['att2'] = np.nan
  43. df['att2'] = df['att2'].mask(df['elagin_attractiv'] < 4, 0)
  44. df['att2'] = df['att2'].mask(df['elagin_attractiv'] >= 4, 1)
  45. group = df.groupby(['att2'])
  46. for i in group.groups.keys():
  47.     print(f'{i}:{len(group.groups[i])}')
  48.  
  49. #8
  50. print(round(df.groupby(['elagin_attractiv']).age.mean(), 2))
  51.  
  52. df.to_csv('res.csv')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement