标签:存储 长度 插入 合并 1.5 多个 span values 数组
import pandas as pd dates = pd.date_range(‘20170817‘, periods=7)
结果
此时会产生一个日期序列 从2017-08-17开始,产生7个连续的日期
DatetimeIndex([‘2017-08-17‘, ‘2017-08-18‘, ‘2017-08-19‘, ‘2017-08-20‘,‘2017-08-21‘, ‘2017-08-22‘, ‘2017-08-23‘], dtype=‘datetime64[ns]‘, freq=‘D‘)
import pandas as pd import numpy as np dates = pd.date_range(‘20170817‘, periods=7) df = pd.DataFrame(np.random.randn(7,4), index=dates, columns=[‘A‘,‘B‘,‘C‘,‘D‘])
结果
此时会在Dataframe对象里插入7行每行4个随机数(7和4都是由index,columns的长度决定)
A B C D
2017-08-17 -1.056218 0.131798 -0.131886 1.138259
2017-08-18 1.699503 2.494435 1.246487 0.743047
2017-08-19 -0.803859 0.911608 -0.858777 -1.790395
2017-08-20 -0.314994 0.848343 -0.563583 0.337760
2017-08-21 2.139696 -1.633749 1.210869 0.950425
2017-08-22 0.480713 -0.215890 -0.342616 -0.015152
2017-08-23 0.063328 -1.522219 -0.132829 1.352349
df[‘A‘]
注意
当要输出Dataframe对象的index时候们只能用
df.index
df[0:3]
df[‘20170817‘:‘20170819‘]
df.loc[:,[‘A‘,‘B‘]]
注意
此处不能用
df.loc[:,[‘A‘:‘B‘]]
df.loc[dates[0],‘A‘]
df.iloc[1,1]
df.iloc[3]
此时会选择 第4、5行的前两列的数据
df.iloc[3:5,0:2]
df.iloc[[1,2,3],[0,2]]
df[df.A < 0]
df[df > 0]
s1 = pd.Series([1,2,3,4,5,6,7], index=pd.date_range(‘20170817‘, periods=7)) df[‘F‘] = s1
df1.dropna()
df1.fillna(value=100)
注意 ascending=True 升序
df.sort_index(ascending=True)
df.sort_values(‘B‘,ascending=True)
df.mean()
df.mean(1)
df = pd.DataFrame(np.random.randn(10, 4)) pieces = [df[:2], df[3:4]] df2=pd.concat(pieces)
df = pd.DataFrame(np.random.randn(8, 4), columns=[‘A‘,‘B‘,‘C‘,‘D‘]) s = df.iloc[3] df2=df.append(s, ignore_index=True)
ignore_index=True 即:保持原来的index
df = pd.DataFrame({‘A‘: [‘foo‘, ‘bar‘, ‘foo‘, ‘bar‘, ‘foo‘, ‘bar‘, ‘foo‘, ‘foo‘], ‘B‘: [‘one‘, ‘one‘, ‘two‘, ‘three‘, ‘two‘, ‘two‘, ‘one‘, ‘three‘], ‘C‘: np.random.randn(8), ‘D‘: np.random.randn(8)})
df.groupby(‘A‘)[‘C‘].sum() # 只显示C df.groupby(‘A‘).sum() # 显示所有
df.groupby([‘A‘, ‘B‘]).sum()
# 从 csv 文件读取数据 pd.read_csv(‘A.csv‘) # 保存到 csv 文件 df.to_csv(‘A.csv‘) # 读取 excel 文件 pd.read_excel(‘A.xlsx‘, ‘Sheet1‘, index_col=None, na_values=[‘NA‘]) # 保存到 excel 文件 df.to_excel(‘A.xlsx‘, sheet_name=‘Sheet1‘)
标签:存储 长度 插入 合并 1.5 多个 span values 数组
原文地址:http://www.cnblogs.com/HapyyHao1314/p/7383783.html