Pandas基础1

该博客介绍了Pandas基础,包括文件读写(csv, txt, xls/xlsx)、Series和DataFrame的基本操作(创建、索引、修改、选择列等)、常用函数(head/tail, unique/nunique, count/value_counts等)以及排序方法。还提供了问题与练习,如统计美剧《权力的游戏》中人物和台词数量,分析科比投篮数据。" 131293114,13401669,SQL Server 动态行列转换,"['数据库', 'SQLServer', '数据处理', '数据转换']
摘要由CSDN通过智能技术生成

第一章 Pandas基础

import pandas as pd 
import numpy as np
pd.__version__

'1.0.3'

一、文件的读取与写入

csv

#df = pd.read_csv(r'data\table.csv')
df = pd.read_csv('data/table.csv')
df.head(10)#默认5行
#与ipynb放在一个文件

df.to_csv('data/new_table.csv')
#df.to_csv('data/new_table.csv', header=False) #保存时除去列索引
#df.to_csv('data/new_table.csv', index=False) #保存时除去行索引

txt

txt = pd.read_table('data/table.txt')#可设置sep分隔符参数
txt

txt.to_csv('data/new_table.txt',index=False,sep='\t')

xls, xlsx

#需要安装xlrd包
df_excel = pd.read_excel('data/table.xlsx')
df_excel.head()

#需要安装openpyxl
df.to_excel('data/new_table2.xlsx', sheet_name='Sheet1')

二、基本数据结构

Series

s = pd.Series(np.random.randn(5),index=['a','b','c','d','e'],name='这是一个Series',dtype='float64')#numpy.random.randn(n)返回n个数具有正态分布

s.values
s.name
s.index
s.dtype
s['a']#取出一个元素
a.mean()#均值
print([attr for attr in dir(s) if not attr.startswith('_')])
['T', 'a', 'abs', 'add', 'add_prefix', 'add_suffix', 'agg', 'aggregate', 'align', 'all', 'any', 'append', 'apply', 'argmax', 'argmin', 'argsort', 'array', 'asfreq', 'asof', 'astype', 'at', 'at_time', 'attrs', 'autocorr', 'axes', 'b', 'between', 'between_time', 'bfill', 'bool', 'c', 'clip', 'combine', 'combine_first', 'convert_dtypes', 'copy', 'corr', 'count', 'cov', 'cummax', 'cummin', 'cumprod', 'cumsum', 'd', 'describe', 'diff', 'div', 'divide', 'divmod', 'dot', 'drop', 'drop_duplicates', 'droplevel', 'dropna', 'dtype', 'dtypes', 'duplicated', 'e', 'empty', 'eq', 'equals', 'ewm', 'expanding', 'explode', 'factorize', 'ffill', 'fillna', 'filter', 'first', 'first_valid_index', 'floordiv', 'ge', 'get', 'groupby', 'gt', 'hasnans', 'head', 'hist', 'iat', 'idxmax', 'idxmin', 'iloc', 'index', 'infer_objects', 'interpolate', 'is_monotonic', 'is_monotonic_decreasing', 'is_monotonic_increasing', 'is_unique', 'isin', 'isna', 'isnull', 'item', 'items', 'iteritems', 'keys', 'kurt', 'kurtosis', 'last', 'last_valid_index', 'le', 'loc', 'lt', 'mad', 'map', 'mask', 'max', 'mean', 'median', 'memory_usage', 'min', 'mod', 'mode', 'mul', 'multiply', 'name', 'nbytes', 'ndim', 'ne', 'nlargest', 'notna', 'notnull', 'nsmallest', 'nunique', 'pct_change', 'pipe', 'plot', 'pop', 'pow', 'prod', 'product', 'quantile', 'radd', 'rank', 'ravel', 'rdiv', 'rdivmod', 'reindex', 'reindex_like', 'rename', 'rename_axis', 'reorder_levels', 'repeat', 'replace', 'resample', 'reset_index', 'rfloordiv', 'rmod', 'rmul', 'rolling', 'round', 'rpow'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值