阅读 87

Pandas 学习笔记

1. 显示最大列数

pd.set_option('display.max_columns', 80)

2.显示总行数

df.shape[0]
len(df)

3.查看数据类型

df.dtypes

4.获取unique及其个数

df['A'].unique()
len(df['A'].unique())

5.去掉全為NaN的行

df.dropna(how='all')

6.去掉全為NaN的列

df.dropna(axis=1, how='all')

7.使用前一行的值填充,並限制2行

          0         1         2
0  0.476985  3.248944 -1.021228
1 -0.577087  0.124121  0.302614
2  0.523772       NaN  1.343810
3 -0.713544       NaN -2.370232
4 -1.860761       NaN       NaN
5 -1.265934       NaN       NaN
df.fillna(method='ffill', limit=2)
         0         1         2
0  0.476985  3.248944 -1.021228
1 -0.577087  0.124121  0.302614
2  0.523772  0.124121  1.343810
3 -0.713544  0.124121 -2.370232
4 -1.860761       NaN -2.370232
5 -1.265934       NaN -2.370232

8.查看前5行和后5行

df.head().append(df.tail())

9.df.info()函数可以快速查看是否存在缺失值情况

10.df. loc[*, *]

其中第一个 *代表行的选择,第二个* 代表列的选择,如果省略第二个位置写作loc[],这个 * 是指行的筛选。

11. df.groupby(m)[n].k

其中:
m: 分组依据,但需要按照多个条件分组时,需要把条件放到一个列表中
n: 数据来源,即需要计算的字段,同样的,需要多个字段也是放入一个列表
k: 聚合函数,常用的有min/max/mean/count等,也可以传入自定义参数

12. pivot 与pivot_table

pivot把一个长表转为宽表, 其中index必须具有唯一性。
pivot_tablepivot多了一个aggfunc参数,即聚合参数。
pivot:无法聚合,只能简单重塑(reshape),如果存在重复数据将会报错;常用于处理非数字数据。
pivot_table:可以聚合,正好弥补 pivot 的缺陷。

13.Merge的4种连接方式


使用merge时,如果没有指定 on = 哪一列,则默认以重叠列名当做链接键, 当然也可以按照多键连接,只需要'on'参数后传入多键列表即可

14.身份证发征地和当前行政区域查询

def get_location(id_number):
#     print(id_number)
    url = f'https://qq.ip138.com/idsearch/index.asp?userid={id_number}&action=idcard'
    headers = {
        'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Mobile Safari/537.36'
    }
    res = requests.get(url,headers = headers)
    html  = res.text.encode("ISO-8859-1").decode("utf-8")
    element = etree.HTML(html)
    try:
        issue_place = element.xpath("//div[@class='bd']/table/tbody/tr[5]/td[2]/p/text()")[0]
        current_place = element.xpath("//div[@class='bd']/table/tbody/tr[6]/td[2]/p/text()")[0]
    except Exception as e :
        current_place = issue_place
    return issue_place,current_place

df[['发证地区','行政区域']] = df.apply(lambda row:pd.Series(get_location(row['身份证号'])),axis=1)

15. 设置category

df["Status"] = df["Status"].astype("category")
 
df["Status"].cat.set_categories(["won","pending","presented","declined"],inplace=True)

16.获取除去某列的其他所有列

df.loc[:,df.columns != 'column_name' ]

17.查询只出现在left dataframe里的

(left.merge(right, on='key', how='left', indicator=True)
     .query('_merge == "left_only"')
     .drop('_merge', axis = 1))

18.根据第一个excel修改其他excel文件列表并 append在一起

dfs = []       
for i,f in enumerate(files):  # files 是excel 文件路径
    df = pd.read_excel(f)
    if i == 0:
        col = df.columns
    df.columns=col
    dfs.append(df)

19. 统计每列nan总数

df.isnull().sum()

作者:东东隆东抢

原文链接:https://www.jianshu.com/p/d6de50cbf4b5

文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐