python数据处理常用方法

1、读取指定sheet和指定列的内容

data = pd.read_excel(“data.xlsx”,sheet_name=”Sheet1″,usecols=[“year”,”code1″,”code2″,”name”,”keywords”,”type”,’new’])

 

2、遍历数据,对两列数据相似度比较

for i in range(0,75065):
    if data.loc[i][‘code2’]!=0:
        if data.loc[i][‘code1’][:5]==data.loc[i][‘code2’][:5]:
            data.loc[i,’inter’]=0
        elif data.loc[i][‘code1’][:3]==data.loc[i][‘code2’][:3]:
            data.loc[i,’inter’]=1
        elif data.loc[i][‘code1’][:1]==data.loc[i][‘code2’][:1]:
            data.loc[i,’inter’]=2
        else:
            data.loc[i,’inter’]=3

3、遍历数据,统计每类分类变量的数量

list= [‘fund’,’year’,’inter’,’age’,’degree’,’title’,’institute’,’economy’,’gender’,’type’]
for i in range(0,10):
    print(data[list[i]].value_counts())

4、遍历数据,删除某列为特定值的数据

df_clear = data.drop(data[data[‘discipline’]==”H”].index)

5、正则去掉字符串左边或者右边的内容

import re
template = “DF’,’17340′,’http://www.zgglkx.com’,’2021′,’205′)”

delete_left = template.lstrip(‘”DF’)
print(delete_left)

delete_right = template.rstrip(‘205′)’)
print(delete_right)

0

评论0

请先
显示验证码
没有账号?注册  忘记密码?