python数据处理常用方法(四)

13、字符串的连接

name = 10
height =100
print(f”I am {name} years old and I am {height} cm.”)

14、多个空格替换成其他符号

import re
str1 = ‘2020    第一卷         第五期’
str2 = re.sub(‘ +’, ‘;’, str1)
print(str2)

结果:
“`python
2020;第一卷;第五期

15、获取某个字符串左边/右边的内容

string1 = string[0:string.rfind(‘[‘)]

string2 = string[string.rfind(‘[‘)+1:]

16、四舍五入和大小比较

def ceil(*args, **kwargs): # real signature unknown
    “””
    Return the ceiling of x as an Integral.
    This is the smallest integer >= x.
    “””
    pass

17、读取某列为指定内容的所有行

data = data.loc[data[“year”]==2016]

18、统计作者数量并增加为新的列

for i in range(2960):
    data.loc[i,’author_num’]=len(data.loc[i][‘AU’].split(“;”))

19、将某一列连续变量虚拟化

data[‘AP’]=data.apply(lambda x:1 if x[‘DT’] == “Article; Proceedings Paper” else 0,axis=1)

20、删去某列为指定内容的行

data= data.drop(data[data[‘DT’]==”Review”].index)

21、判断某列中每行的值,并且做出替代

data[‘AP’]=data.apply(lambda x:1 if x[‘DT’] == “Article; Proceedings Paper” else 0,axis=1)

22、去掉某列中为空值的行

data1= data1.dropna(axis=0,subset = [“ID_num”]) 

 

 

 

 

 

 

 

0

评论0

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