pandas方法总结

该代码是总结的一些pandas用法,方法暂时不全,后续会继续添加,其中代码主要参考了pandas中文文档和B站视频。

https://www.bilibili.com/video/BV1k94y1975j?vd_source=7771577bd8c0c69d43ee27a1c1ac8a1a

import pandas as pd
import numpy as np

转为DataFrame

list = {
    'name':['a','b','c','d','e'],
    'age':[11,23,55,31,11],
    'sex':[1,0,1,1,0]
}

pd_list = pd.DataFrame(list)
pd_list
nameagesex
0a111
1b230
2c551
3d311
4e110

获取某一行

name_a = pd_list[pd_list['name']=='a']
name_a
nameagesex
0a111
pd_list.loc[3]
name     d
age     31
sex      1
Name: 3, dtype: object

获取列名和行名

pd_list.columns
Index(['name', 'age', 'sex'], dtype='object')
pd_list.index
RangeIndex(start=0, stop=5, step=1)

更改列名

pd_list.rename(columns={'age':'Age'},inplace=True)
pd_list
nameAgesex
0a111
1b230
2c551
3d311
4e110

统计某列相同内容出现次数

#统计不同年龄的人数
pd_list['Age'].value_counts()
11    2
23    1
55    1
31    1
Name: Age, dtype: int64

根据条件获取某列的值

#获取大于30岁的行
pd_list[pd_list['Age']>30]
nameAgesex
2c551
3d311

计数某列平均值

# 计数年龄平均值
pd_list['Age'].mean()
26.2

panda转为列表

age_list = pd_list['Age'].to_list()
age_list
[11, 23, 55, 31, 11]

去除某列重复值

list = {
    'name':['a','b','c','d','e','f','g','h'],
    'age':[11,23,55,31,11,11,np.nan,34],
    'sex':[1,0,1,1,0,1,1,0]
}
pd_list = pd.DataFrame(list)
pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00
pd_list.drop_duplicates(['age'])
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
6gNaN1
7h34.00

保存到Excel

pd_list.to_excel('list.xlsx')

查看数据列数行数

pd_list.shape
(8, 3)
#行数
pd_list.shape[0]
8
#列数
pd_list.shape[1]
3

交换两列位置

pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00
#第二列和第三列交换
swa = pd_list.columns[[2,1]]
pd_listd =pd_list[swa]
pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00

找到限定条件的某行

#找年龄最大的行
pd_list[pd_list['age']==pd_list['age'].max()]
nameagesex
2c55.01

查看前几行和后几行

pd_list.head(3)
nameagesex
0a11.01
1b23.00
2c55.01
pd_list.tail(3)
nameagesex
5f11.01
6gNaN1
7h34.00

删除某行

#删除index为3的行
pd_list.drop(index=3,inplace=True)
pd_list
nameagesex
0a11.01
1b23.00
2c55.01
4e11.00
5f11.01
6gNaN1
7h34.00

添加一行数据 有问题

pd_list.loc[len(pd_list.index)] = ['ww', 17, 0]
pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00
8ww17.00
pd_list = pd_list.append({'name':'kw','age':15,'sex':1}, ignore_index = True)
pd_list
C:\AppData\AppData\Local\Temp\ipykernel_13232\1113817121.py:1: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  pd_list = pd_list.append({'name':'kw','age':15,'sex':1}, ignore_index = True)
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00
8ww17.00
9kw15.01
pd_list = pd_list.insert({'name':'kw','age':15,'sex':1}, ignore_index = True)
pd_list

以某列为标准进行排序

pd = pd_list.sort_values('age')
pd
nameagesex
0a11.01
4e11.00
5f11.01
1b23.00
7h34.00
2c55.01
6gNaN1
pd = pd_list.sort_values('age',ascending=False)
pd
nameagesex
2c55.01
7h34.00
1b23.00
0a11.01
4e11.00
5f11.01
6gNaN1

读取本地文件

frame = pd.read_excel('路径/文件名')
frame = pd.read_csv('路径/文件名')

查看索引,数据类型,内存,数据列汇总,每列数据类型

pd_list.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 7 entries, 0 to 7
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   name    7 non-null      object 
 1   age     6 non-null      float64
 2   sex     7 non-null      int64  
dtypes: float64(1), int64(1), object(1)
memory usage: 224.0+ bytes
pd_list.describe()
agesex
count6.0000007.000000
mean24.1666670.571429
std17.7134600.534522
min11.0000000.000000
25%11.0000000.000000
50%17.0000001.000000
75%31.2500001.000000
max55.0000001.000000
pd_list.dtypes
name     object
age     float64
sex       int64
dtype: object

添加删除某一列

pd_list['new']=1
pd_list
nameagesexnew
0a11.011
1b23.001
2c55.011
3d31.011
4e11.001
5f11.011
6gNaN11
7h34.001
del pd_list['new']
pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00

合并某两列到新的一列

pd_list['new'] = pd_list['name'].map(str) + pd_list['name']
pd_list
nameagesexnew
0a11.01aa
1b23.00bb
2c55.01cc
3d31.01dd
4e11.00ee
5f11.01ff
6gNaN1gg
7h34.00hh
del pd_list['new']
pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00

查看某列最大值和最小值,以及差

pd_list['age'].max()
55.0
pd_list['age'].min()
11.0
pd_list['age'].max() - pd_list['age'].min()
44.0
pd_list[['age']].apply(lambda x:x.max()-x.min())
age    44.0
dtype: float64

设置索引

pd_list.set_index('name')
agesex
name
a11.01
b23.00
c55.01
d31.01
e11.00
f11.01
gNaN1
h34.00

用loc和iloc进行选取

pd_list
nameagesex
0a11.01
1b23.00
2c55.01
3d31.01
4e11.00
5f11.01
6gNaN1
7h34.00
pd_list.loc[3]
name       d
age     31.0
sex        1
Name: 3, dtype: object
pd_list.iloc[3]
name       d
age     31.0
sex        1
Name: 3, dtype: object
pd_list.loc[[i for i in range(0,3)],['name','age']]
nameage
0a11.0
1b23.0
2c55.0
pd_list.loc[[3,5],['name','sex']]
namesex
3d1
5f1
pd_list.loc[1:5,['name','sex']]
namesex
1b0
2c1
3d1
4e0
5f1
pd_list.iloc[[3,5],[0,2]]
namesex
3d1
5f1

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

<编程路上>

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值