pandas 删除特定行根据条件_记录21个Pandas技巧

本文介绍了21个基础Pandas技巧,包括从CSV读取数据、创建数据框、查看数据、重命名列、获取列频率、删除列和行、按条件切片数据帧、迭代行、按列排序、应用函数、分组聚合等操作,旨在帮助Pandas初学者掌握数据分析的基本技能。
摘要由CSDN通过智能技术生成
bf6a8a03599e2afaf90e4b792129924c.png

image.png

介绍

Pandas是一个易于使用且功能强大的数据库分析库。与NumPy一样,它可以矢量化大多数基本操作,即使在CPU上也可以并行计算,从而加快计算速度。这里指定的操作非常基础,但如果您刚开始使用Pandas则非常重要。您将被要求将pandas导入为'pd',然后使用'pd'对象执行其他基本的pandas操作。

  1. 如何从CSV文件或文本文件中读取数据?

CSV文件以逗号分隔,因此为了读取CSV文件,请执行以下操作:

df = pd.read_csv(file_path, sep=’,’, header = 0, index_col=False,names=None)Explanation:‘read_csv’ function has a plethora of parameters and I have specified only a few, ones that you may use most often. A few key points:a) header=0 means you have the names of columns in the first row in the file and if you don’t you will have to specify header=Noneb) index_col = False means to not use the first column of the data as an index in the data frame, you might want to set it to true if the first column is really an index.c) names = None implies you are not specifying the column names and want it to be inferred from csv file, which means that your header = some_number contains column names. Otherwise, you can specify the names in here in the same order as you have the data in the csv file. If you are reading a text file separated by space or tab, you could simply change the sep to be:sep = " " or sep=''

2.如何使用预先存在的列或NumPy 2D阵列的字典创建数据框?

使用字典

# c1, c2, c3, c4 are column names. d_dic ={'first_col_name':c1,'second_col_names':c2,'3rd_col_name':c3} df = pd.DataFrame(data = d_dic)

使用NumPy数组


  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值