栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Pandas】Pandas Daframe 常用用法

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Pandas】Pandas Daframe 常用用法

(1)取某列等于某个值的所有行数据

df.loc[df['A']==999]

(2)datetime作为索引取行数据

# 第一种方式
df_index = list(df.index)
for index in df_index:
    tmp = df.loc[[str(index)]]
# 第二种方式
df_index = list(df.index)
for index in df_index:
	tag = df.loc[str(index),'B']

(3)取某列等于某个值的所有行

df = df[df.tag==False]

(4)合并list中的dataframe

df_list = [df1,df2,df3]
all_df = pd.concat(df_list)

(5)将某一列作为index索引

df.set_index(["Column"], inplace=True)

(6)根据index索引排序

df.sort_index(inplace=True)

(7)利用tqdm对一列进行处理

from tqdm import tqdm
tqdm.pandas()

def clearTxt(line):
    if line != '':
        line = line.strip()
        #去除文本中的英文和数字
        line = re.sub("[a-zA-Z0-9]", "", line)
        #去除文本中的中文符号和英文符号
        line = re.sub("[s+.!/_,$%^*(+"';:“”.]+|[+——!,。??、~@#¥%……&*()]+", "", line)
        #分词
        segList = jieba.cut(line, cut_all=False)
        segSentence = ''
        for word in segList:
            if word != 't':
                segSentence += word + " "
    return segSentence.strip()
train_data['Text'].progress_apply(clearTxt)

(8)将city一列拆分为city1和city2两列

df['city1'] = df['city'].map(lambda x:x.split("|")[0])
df['city2'] = df['city'].map(lambda x:x.split("|")[1])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/423655.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号