python 学习第五天补充 字符串的抽取和分割
字符串的抽取
from pandas import read_csv; df = read_csv("D://PA//4.6//data.csv"); df['tel'] = df['tel'].astype(str); #运营商 bands = df['tel'].str.slice(0, 3); #地区 areas = df['tel'].str.slice(3, 7); #号码段 nums = df['tel'].str.slice(7, 11);
字符串的分割
from pandas import Series; from pandas import DataFrame; from pandas import read_csv; df = read_csv("D:\\Python\\3.2\\2.csv"); newDF = df['name'].str.split(' ', 1, True); newDF.columns = ['band', 'name']; #重新定义列名
特定条件的提取
import pandas; from pandas import read_csv; df = read_csv("D://PA//4.8//data.csv", sep="|"); df[df.comments>10000]; df[df.comments.between(1000, 10000)] df[pandas.isnull(df.title)] df[df.title.str.contains('台电', na=False)] df[(df.comments>=1000) & (df.comments<=10000)]
随机抽取
import numpy; from pandas import read_csv; df = read_csv("D://PA//4.9//data.csv"); #numpy.random.randint(start,end,num) ,其中start表示范围的开始值,end表示范围的结束值,num随机抽样个数 r = numpy.random.randint(0, 10, 3); df.loc[r, :];