Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

03_merge_loan_info.py 1.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  1. '''
  2. Merges the various loan info csvs
  3. '''
  4. import os
  5. import pandas as pd
  6. from lendingclub import config
  7. import j_utils.munging as mg
  8. # Get the loan_info csvs to iterate over ______________________________________
  9. files = os.listdir(config.wrk_csv_dir)
  10. print("merging the separate loan info csvs")
  11. loan_info_files = [
  12. file_ for file_ in files
  13. if not (file_.startswith('.') | file_.startswith('lc_') |
  14. file_.startswith('PMTHIST') | file_.startswith('LCData'))
  15. ]
  16. to_concat = []
  17. for file_ in loan_info_files:
  18. print('loading in {0}'.format(file_))
  19. to_concat.append(
  20. pd.read_csv(os.path.join(config.wrk_csv_dir, file_),
  21. header=1,
  22. engine='python',
  23. skipfooter=2))
  24. loan_info = pd.concat(to_concat)
  25. # Block to ensure that rows that aren't actually loans are dropped ____________
  26. # All loans must have int/term/funded
  27. loan_info = loan_info[loan_info['term'].notnull()]
  28. loan_info['int_rate'] = loan_info['int_rate'].str.strip('%').astype(float)
  29. loan_info['term'] = loan_info['term'].str[:3].astype(int)
  30. loan_info = loan_info[(loan_info['int_rate'] > 0) & (loan_info['term'] > 0) &
  31. (loan_info['funded_amnt'] > 0)]
  32. # compress memory
  33. changed_type_cols, loan_info = mg.reduce_memory(loan_info)
  34. # Reset index and set id to int______________________
  35. loan_info.reset_index(drop=True, inplace=True)
  36. loan_info['id'] = loan_info['id'].astype(int)
  37. # trying out feather data format
  38. fname = 'raw_loan_info.fth'
  39. loan_info.to_feather(os.path.join(config.data_dir, fname))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...