import csv
import matplotlib.pyplot as plt
import numpy as np


in_1_name = "loan_data.csv"
in_2_name = "home_ownership_data.csv"
out_name = "merged.csv"

with open(in_1_name, 'r') as in_1, open(in_2_name, 'r') as in_2, open(out_name, 'w') as out:
    reader1 = csv.DictReader(in_1)
    reader2 = csv.DictReader(in_2)
    fieldnames = ['member_id', 'loan_amnt', 'home_ownership']
    writer = csv.DictWriter(out, fieldnames=fieldnames)
    writer.writeheader()
    for row1 in reader1:
        in_2.seek(0)
        for row2 in reader2:
            if row1['member_id'] == row2['member_id']:
                writer.writerow({'member_id': row1['member_id'] , 'loan_amnt': row1['loan_amnt'], 'home_ownership': row2['home_ownership']})
                break

mortgage_loan = rent_loan = own_loan = mortgage_counter = rent_counter = own_counter = 0
data = "merged.csv"
with open (data, 'r') as data:
    reader = csv.DictReader(data)
    for row in reader:
        if row['home_ownership'] == "MORTGAGE":
            mortgage_loan += int(row['loan_amnt'])
            mortgage_counter += 1
        elif row['home_ownership'] == "RENT":
            rent_loan += int(row['loan_amnt'])
            rent_counter += 1
        elif row['home_ownership'] == "OWN":
            own_loan += int(row['loan_amnt'])
            own_counter += 1

mortgage_avg = mortgage_loan/mortgage_counter
rent_avg = rent_loan/rent_counter
own_avg = own_loan/own_counter


fig, axs = plt.subplots(2, 1, figsize=(6, 7))
plt.title('Average loan amounts per home ownership')
x_axis = ['MORTGAGE', 'OWN', 'RENT']
y_axis = [mortgage_avg, own_avg, rent_avg]
plt.xlabel('Home ownership')
plt.ylabel('Average loan amount($)')
columns = ["home_ownership", "loan_amnt"]
rows = ['0', '1', '2']
cell_text = np.array((['MORTGAGE', y_axis[0]], ['OWN', y_axis[1]], ['RENT', y_axis[2]]))
axs[0].axis('tight')
axs[0].axis('off')
the_table = axs[0].table(cellText=cell_text, rowLabels=rows, colLabels=columns, loc='center')
axs[1].bar(x_axis, y_axis, align='center', color='steelblue')
plt.show()
1bfcdb70344b3142fd8b6c57c2edc62ee4856407
<mask token>

<mask token>
with open(in_1_name, 'r') as in_1, open(in_2_name, 'r') as in_2, open(out_name,
    'w') as out:
    reader1 = csv.DictReader(in_1)
    reader2 = csv.DictReader(in_2)
    fieldnames = ['member_id', 'loan_amnt', 'home_ownership']
    writer = csv.DictWriter(out, fieldnames=fieldnames)
    writer.writeheader()
    for row1 in reader1:
        in_2.seek(0)
        for row2 in reader2:
            if row1['member_id'] == row2['member_id']:
                writer.writerow({'member_id': row1['member_id'],
                    'loan_amnt': row1['loan_amnt'], 'home_ownership': row2[
                    'home_ownership']})
                break
<mask token>
with open(data, 'r') as data:
    reader = csv.DictReader(data)
    for row in reader:
        if row['home_ownership'] == 'MORTGAGE':
            mortgage_loan += int(row['loan_amnt'])
            mortgage_counter += 1
        elif row['home_ownership'] == 'RENT':
            rent_loan += int(row['loan_amnt'])
            rent_counter += 1
        elif row['home_ownership'] == 'OWN':
            own_loan += int(row['loan_amnt'])
            own_counter += 1
<mask token>
plt.title('Average loan amounts per home ownership')
<mask token>
plt.xlabel('Home ownership')
plt.ylabel('Average loan amount($)')
<mask token>
axs[0].axis('tight')
axs[0].axis('off')
<mask token>
axs[1].bar(x_axis, y_axis, align='center', color='steelblue')
plt.show()

<mask token>
in_1_name = 'loan_data.csv'
in_2_name = 'home_ownership_data.csv'
out_name = 'merged.csv'
with open(in_1_name, 'r') as in_1, open(in_2_name, 'r') as in_2, open(out_name,
    'w') as out:
    reader1 = csv.DictReader(in_1)
    reader2 = csv.DictReader(in_2)
    fieldnames = ['member_id', 'loan_amnt', 'home_ownership']
    writer = csv.DictWriter(out, fieldnames=fieldnames)
    writer.writeheader()
    for row1 in reader1:
        in_2.seek(0)
        for row2 in reader2:
            if row1['member_id'] == row2['member_id']:
                writer.writerow({'member_id': row1['member_id'],
                    'loan_amnt': row1['loan_amnt'], 'home_ownership': row2[
                    'home_ownership']})
                break
(mortgage_loan) = (rent_loan) = (own_loan) = (mortgage_counter) = (rent_counter
    ) = (own_counter) = 0
data = 'merged.csv'
with open(data, 'r') as data:
    reader = csv.DictReader(data)
    for row in reader:
        if row['home_ownership'] == 'MORTGAGE':
            mortgage_loan += int(row['loan_amnt'])
            mortgage_counter += 1
        elif row['home_ownership'] == 'RENT':
            rent_loan += int(row['loan_amnt'])
            rent_counter += 1
        elif row['home_ownership'] == 'OWN':
            own_loan += int(row['loan_amnt'])
            own_counter += 1
mortgage_avg = mortgage_loan / mortgage_counter
rent_avg = rent_loan / rent_counter
own_avg = own_loan / own_counter
fig, axs = plt.subplots(2, 1, figsize=(6, 7))
plt.title('Average loan amounts per home ownership')
x_axis = ['MORTGAGE', 'OWN', 'RENT']
y_axis = [mortgage_avg, own_avg, rent_avg]
plt.xlabel('Home ownership')
plt.ylabel('Average loan amount($)')
columns = ['home_ownership', 'loan_amnt']
rows = ['0', '1', '2']
cell_text = np.array((['MORTGAGE', y_axis[0]], ['OWN', y_axis[1]], ['RENT',
    y_axis[2]]))
axs[0].axis('tight')
axs[0].axis('off')
the_table = axs[0].table(cellText=cell_text, rowLabels=rows, colLabels=
    columns, loc='center')
axs[1].bar(x_axis, y_axis, align='center', color='steelblue')
plt.show()

import csv
import matplotlib.pyplot as plt
import numpy as np
in_1_name = 'loan_data.csv'
in_2_name = 'home_ownership_data.csv'
out_name = 'merged.csv'
with open(in_1_name, 'r') as in_1, open(in_2_name, 'r') as in_2, open(out_name,
    'w') as out:
    reader1 = csv.DictReader(in_1)
    reader2 = csv.DictReader(in_2)
    fieldnames = ['member_id', 'loan_amnt', 'home_ownership']
    writer = csv.DictWriter(out, fieldnames=fieldnames)
    writer.writeheader()
    for row1 in reader1:
        in_2.seek(0)
        for row2 in reader2:
            if row1['member_id'] == row2['member_id']:
                writer.writerow({'member_id': row1['member_id'],
                    'loan_amnt': row1['loan_amnt'], 'home_ownership': row2[
                    'home_ownership']})
                break
(mortgage_loan) = (rent_loan) = (own_loan) = (mortgage_counter) = (rent_counter
    ) = (own_counter) = 0
data = 'merged.csv'
with open(data, 'r') as data:
    reader = csv.DictReader(data)
    for row in reader:
        if row['home_ownership'] == 'MORTGAGE':
            mortgage_loan += int(row['loan_amnt'])
            mortgage_counter += 1
        elif row['home_ownership'] == 'RENT':
            rent_loan += int(row['loan_amnt'])
            rent_counter += 1
        elif row['home_ownership'] == 'OWN':
            own_loan += int(row['loan_amnt'])
            own_counter += 1
mortgage_avg = mortgage_loan / mortgage_counter
rent_avg = rent_loan / rent_counter
own_avg = own_loan / own_counter
fig, axs = plt.subplots(2, 1, figsize=(6, 7))
plt.title('Average loan amounts per home ownership')
x_axis = ['MORTGAGE', 'OWN', 'RENT']
y_axis = [mortgage_avg, own_avg, rent_avg]
plt.xlabel('Home ownership')
plt.ylabel('Average loan amount($)')
columns = ['home_ownership', 'loan_amnt']
rows = ['0', '1', '2']
cell_text = np.array((['MORTGAGE', y_axis[0]], ['OWN', y_axis[1]], ['RENT',
    y_axis[2]]))
axs[0].axis('tight')
axs[0].axis('off')
the_table = axs[0].table(cellText=cell_text, rowLabels=rows, colLabels=
    columns, loc='center')
axs[1].bar(x_axis, y_axis, align='center', color='steelblue')
plt.show()

import csv
import matplotlib.pyplot as plt
import numpy as np


in_1_name = "loan_data.csv"
in_2_name = "home_ownership_data.csv"
out_name = "merged.csv"

with open(in_1_name, 'r') as in_1, open(in_2_name, 'r') as in_2, open(out_name, 'w') as out:
    reader1 = csv.DictReader(in_1)
    reader2 = csv.DictReader(in_2)
    fieldnames = ['member_id', 'loan_amnt', 'home_ownership']
    writer = csv.DictWriter(out, fieldnames=fieldnames)
    writer.writeheader()
    for row1 in reader1:
        in_2.seek(0)
        for row2 in reader2:
            if row1['member_id'] == row2['member_id']:
                writer.writerow({'member_id': row1['member_id'] , 'loan_amnt': row1['loan_amnt'], 'home_ownership': row2['home_ownership']})
                break

mortgage_loan = rent_loan = own_loan = mortgage_counter = rent_counter = own_counter = 0
data = "merged.csv"
with open (data, 'r') as data:
    reader = csv.DictReader(data)
    for row in reader:
        if row['home_ownership'] == "MORTGAGE":
            mortgage_loan += int(row['loan_amnt'])
            mortgage_counter += 1
        elif row['home_ownership'] == "RENT":
            rent_loan += int(row['loan_amnt'])
            rent_counter += 1
        elif row['home_ownership'] == "OWN":
            own_loan += int(row['loan_amnt'])
            own_counter += 1

mortgage_avg = mortgage_loan/mortgage_counter
rent_avg = rent_loan/rent_counter
own_avg = own_loan/own_counter


fig, axs = plt.subplots(2, 1, figsize=(6, 7))
plt.title('Average loan amounts per home ownership')
x_axis = ['MORTGAGE', 'OWN', 'RENT']
y_axis = [mortgage_avg, own_avg, rent_avg]
plt.xlabel('Home ownership')
plt.ylabel('Average loan amount($)')
columns = ["home_ownership", "loan_amnt"]
rows = ['0', '1', '2']
cell_text = np.array((['MORTGAGE', y_axis[0]], ['OWN', y_axis[1]], ['RENT', y_axis[2]]))
axs[0].axis('tight')
axs[0].axis('off')
the_table = axs[0].table(cellText=cell_text, rowLabels=rows, colLabels=columns, loc='center')
axs[1].bar(x_axis, y_axis, align='center', color='steelblue')
plt.show()