pandas基于Numpy,可以看成是处理文本或者表格数据。pandas中有两个主要的数据结构,其中Series数据结构类似于Numpy中的一维数组,DataFrame类似于多维表格数据结构。
pandas是python数据分析的核心模块。它主要提供了五大功能:
import pandas as pd
import numpy as np
import pandas as pd
arr = np.array([1, 2, 3, 4, np.nan, ])
s = pd.Series(arr)
print(s)
0 1.0
1 2.0
2 3.0
3 4.0
4 NaN
dtype: float64
dates = pd.date_range('20190101', periods=6)
print(dates)
np.random.seed(1)
arr = 10*np.random.randn(6, 4)
print(arr)
df = pd.DataFrame(arr, index=dates, columns=['c1', 'c2', 'c3', 'c4'])
df
c1 | c2 | c3 | c4 | |
---|---|---|---|---|
2019-01-01 | 16.243454 | -6.117564 | -5.281718 | -10.729686 |
2019-01-02 | 8.654076 | -23.015387 | 17.448118 | -7.612069 |
2019-01-03 | 3.190391 | -2.493704 | 14.621079 | -20.601407 |
2019-01-04 | -3.224172 | -3.840544 | 11.337694 | -10.998913 |
2019-01-05 | -1.724282 | -8.778584 | 0.422137 | 5.828152 |
2019-01-06 | -11.006192 | 11.447237 | 9.015907 | 5.024943 |
df2 = pd.DataFrame({'a': 1, 'b': [2, 3], 'c': np.arange(2), 'd': 'hello'})
df2 # 字典的key就变成了cloumns
a | b | c | d | |
---|---|---|---|---|
0 | 1 | 2 | 0 | hello |
1 | 1 | 3 | 1 | hello |
属性 | 详解 |
---|---|
dtype | 查看数据类型 |
index | 查看行序列或者索引 |
columns | 查看各列的标签 |
values | 查看数据框内的数据,也即不含表头索引的数据 |
describe | 查看数据每一列的极值,均值,中位数,只可用于数值型数据 |
transpose | 转置,也可用T来操作 |
sort_index | 排序,可按行或列index排序输出 |
sort_values | 按数据值来排序 |
DataFrame取值
df[‘c2‘]
:取c2一列df[0:3]
:取前三行df.loc[‘2019-01-01‘:‘2019-01-05‘]
print(df.iloc[2, 1])
通过索引取值df.iloc[1:4, 1:4]
df.values
取所有的值df[df[‘c1‘] > 0]
使用逻辑判断取值DataFrame值替换
df.iloc[0:3, 0:2] = 0
df[df[‘c1‘] > 0]=100
使用逻辑判断替换读取CSV文件/逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号from io import StringIO
from io import StringIO
test_data = '''
5.1,,1.4,0.2
4.9,3.0,1.4,0.2
4.7,3.2,,0.2
7.0,3.2,4.7,1.4
6.4,3.2,4.5,1.5
6.9,3.1,4.9,
,,,
'''
test_data = StringIO(test_data)
df = pd.read_csv(test_data, header=None)
df.columns = ['c1', 'c2', 'c3', 'c4']
print(df)
导入导出excel表格
import pandas as pd
df = pd.read_excel(filename) #读取文件
df.to_excel(filename) #保存文件
import pandas as pd
from io import StringIO
dic={f'group{k}':[v,v+1,v**2] for k,v in enumerate(range(6))}
df=pd.DataFrame(dic,index=['a','b','c'])
df.to_excel('test.xlsx') #保存文件
df1=pd.read_excel('test.xlsx') #读取文件
print(df1)
pandas读取json文件
strtext = '[{"ttery":"min","issue":"20130801-3391","code":"8,4,5,2,9","code1":"297734529","code2":null,"time":1013395466000},{"ttery":"min","issue":"20130801-3390","code":"7,8,2,1,2","code1":"298058212","code2":null,"time":1013395406000},{"ttery":"min","issue":"20130801-3389","code":"5,9,1,2,9","code1":"298329129","code2":null,"time":1013395346000},{"ttery":"min","issue":"20130801-3388","code":"3,8,7,3,3","code1":"298588733","code2":null,"time":1013395286000},{"ttery":"min","issue":"20130801-3387","code":"0,8,5,2,7","code1":"298818527","code2":null,"time":1013395226000}]'
df = pd.read_json(strtext, orient='records')
print(df)
df.to_excel('pandas处理json.xlsx',
index=False,
columns=["ttery", "issue", "code", "code1", "code2", "time"])
code | code1 | code2 | issue | time | ttery | |
---|---|---|---|---|---|---|
0 | 8,4,5,2,9 | 297734529 | NaN | 20130801-3391 | 1013395466000 | min |
1 | 7,8,2,1,2 | 298058212 | NaN | 20130801-3390 | 1013395406000 | min |
2 | 5,9,1,2,9 | 298329129 | NaN | 20130801-3389 | 1013395346000 | min |
3 | 3,8,7,3,3 | 298588733 | NaN | 20130801-3388 | 1013395286000 | min |
4 | 0,8,5,2,7 | 298818527 | NaN | 20130801-3387 | 1013395226000 | min |
原文:https://www.cnblogs.com/yellowcloud/p/11178127.html