//data.loc[conditions,clomns]
data.loc[(data["gender"]=="Female")&(data["Education"=="Not Graduate")&(data["Loan_Status"]=="Y") , ["Gender","Education","Loan_Status"]]



#创建一个新函数:
def num_missing(x):
  return sum(x.isnull())

#Apply到每一列:
print "Missing values per column:"
print data.apply(num_missing, axis=0) #axis=0代表函数应用于每一列

#Apply到每一行:
print "\nMissing values per row:"
print data.apply(num_missing, axis=1).head() #axis=1代表函数应用于每一行



#首先导入一个寻找众数的函数:
from scipy.stats import mode
mode(data['Gender'])



#值替换:
data['Gender'].fillna(mode(data['Gender']).mode[0], inplace=True)
data['Married'].fillna(mode(data['Married']).mode[0], inplace=True)
data['Self_Employed'].fillna(mode(data['Self_Employed']).mode[0], inplace=True)

#再次检查缺失值以确认:
print data.apply(num_missing, axis=0)



#Determine pivot table
impute_grps = data.pivot_table(values=["LoanAmount"], index=["Gender","Married","Self_Employed"], aggfunc=np.mean)
print impute_grps



#只在带有缺失值的行中迭代:

for i,row in data.loc[data['LoanAmount'].isnull(),:].iterrows():
  ind = tuple([row['Gender'],row['Married'],row['Self_Employed']])
  data.loc[i,'LoanAmount'] = impute_grps.loc[ind].values[0]

#再次检查缺失值以确认:
print data.apply(num_missing, axis=0)



pd.crosstab(data["Credit_History"],data["Loan_Status"],margins=True)



def percConvert(ser):
  return ser/float(ser[-1])

pd.crosstab(data["Credit_History"],data["Loan_Status"],margins=True).apply(percConvert, axis=1)



prop_rates = pd.DataFrame([1000, 5000, 12000], index=['Rural','Semiurban','Urban'],columns=['rates'])



data_merged = data.merge(right=prop_rates, how='inner',left_on='Property_Area',right_index=True, sort=False)
data_merged.pivot_table(values='Credit_History',index=['Property_Area','rates'], aggfunc=len)



data_sorted = data.sort_values(['ApplicantIncome','CoapplicantIncome'], ascending=False)
data_sorted[['ApplicantIncome','CoapplicantIncome']].head(10)



data.boxplot(column="ApplicantIncome",by="Loan_Status")
data.hist(column="ApplicantIncome",by="Loan_Status",bins=30)



#分箱:

def binning(col, cut_points, labels=None):
  #Define min and max values:
  minval = col.min()
  maxval = col.max()
  #利用最大值和最小值创建分箱点的列表
  break_points = [minval] + cut_points + [maxval]
  #如果没有标签,则使用默认标签0 ... (n-1)
  if not labels:
    labels = range(len(cut_points)+1)
  #使用pandas的cut功能分箱
  colBin = pd.cut(col,bins=break_points,labels=labels,include_lowest=True)
  return colBin

#为年龄分箱:
cut_points = [90,140,190]
labels = ["low","medium","high","very high"]
data["LoanAmount_Bin"] = binning(data["LoanAmount"], cut_points, labels)
print pd.value_counts(data["LoanAmount_Bin"], sort=False)



#使用Pandas replace函数定义新函数:
def coding(col, codeDict):
  colCoded = pd.Series(col, copy=True)
  for key, value in codeDict.items():
    colCoded.replace(key, value, inplace=True)
  return colCoded

#把贷款状态LoanStatus编码为Y=1, N=0:
print 'Before Coding:'
print pd.value_counts(data["Loan_Status"])
data["Loan_Status_Coded"] = coding(data["Loan_Status"], {'N':0,'Y':1})
print '\nAfter Coding:'
print pd.value_counts(data["Loan_Status_Coded"])


data.dtypes

#载入文件:
colTypes = pd.read_csv('datatypes.csv')
print colTypes


#迭代每行,指派变量类型。
#注,astype用来指定变量类型。
for i, row in colTypes.iterrows(): #i: dataframe索引; row: 连续的每行  
  if row['feature']=="categorical":
    data[row['feature']]=data[row['feature']].astype(np.object)
  elif row['feature']=="continuous":
    data[row['feature']]=data[row['feature']].astype(np.float)
  print data.dtypes