!/usr/bin/env python
coding: utf-8
In[63]:
import pandas as pd
import numpy as np
import pymysql
conn=pymysql.connect(host="10.101.2.32",user="chenqianguang",passwd="select20",db='clx_loan')
sql=''''''
yx=pd.read_sql_query(sql,conn)
yx
In[65]:
import pymysql
import pandas as pd
from sqlalchemy import create_engine
conn = pymysql.connect(host='10.101.2.41',user = "select_fk",passwd = "select_fk#2022", db = "chenqianguang")
读取数据
建立连接,username替换为用户名,passwd替换为密码,test替换为数据库名
conn = create_engine('mysql+pymysql://select_fk:select_fk#[email protected]:3306/chenqianguang',encoding='utf8')
写入数据,table_name为表名,‘replace’表示如果同名表存在就替换掉
yx.to_sql("dpt50", conn, if_exists='replace', index=False)
In[ ]:
In[ ]:
In[ ]:
In[66]:
import pandas as pd
import numpy as np
df2=pd.DataFrame({'typle1':[2,3,4,4,4,4,4],'month1':[9,3,5,5,6,5,6],
"amount1":[3,5,5,5,6,np.nan,6]})
df2
In[50]:
df2['合计']=df2.iloc[:,:].sum(axis=1)
for i in df2.columns[:]:
df2[i] = df2[i]/df2['合计']
df2
In[55]:
df2.loc['Row_sum'] = df2.iloc[:,:].apply(lambda x: x.sum())
tmp1=df2.iloc[:-1,:]/df2.iloc[:-1,:].sum()
tmp1
In[ ]:
HX1['本月-训练集']=HX1['本月-训练集'].apply(lambda x:format(x,'.2%'))
In[59]:
import pandas as pd
import numpy as np
fj=pd.DataFrame({'product_name':['A','B','C'],'平均复借次数':[9,3,5],
"复借率":[3,5,5]})
fj
In[60]:
fjs=fj
fjs=pd.DataFrame(fj)
fjs=fjs[['product_name','平均复借次数','复借率']].T
import numpy as np
array = np.array(fjs)
list = array.tolist()
list = list[0]
fjs.columns = list
fjs.drop("product_name", inplace=True)
fjs['中文名']=fjs.index
In[67]:
df2
In[ ]:
def qj(x):
values=[0,1,2,5,8,10,15,20,30,50,100,np.inf]
index=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R']
for i in range(len(values)-1):
if values[i]<=x<values[i+1]:
return '{0}_[{1},{2})'.format(index[i],values[i],values[i+1])
return '空值'
BH['近6个月履约贷款次数']=BH['近6个月履约贷款次数'].astype('float')
BH['近6个月履约贷款次数']=BH['近6个月履约贷款次数'].apply(qj)
In[ ]:
python: 3.9.7
sklearn: 0.24.2
sklearn2pmml: 0.90.4
joblib: 1.1.0
sklearn_pandas: 2.2.0
pandas: 1.3.4
numpy: 1.20.3
java: 11.0.11
In[ ]:
In[ ]:
In[ ]:
In[ ]:
发送自 Windows 10 版邮件应用
标签:df2,编辑,np,pd,fjs,import,pandas From: https://www.cnblogs.com/qw45erwqwewqr/p/18167163