Pandas 10分钟入门(官方说明+个人小测试)
来源:互联网 发布:国债期货 知乎 编辑:程序博客网 时间:2024/05/24 23:16
In [19]:
import pandas as pdimport numpy as npimport matplotlib.pyplot as plt
In [20]:
s= pd.Series([1,2,3,np.nan,5,6,]) #series 类型数组。s
Out[20]:
In [21]:
dates= pd.date_range("20170112",periods=6) #Creating a DataFrame by passing a numpy array, with a datetime index and labeled columndates
Out[21]:
In [22]:
list(dates)dates.date
Out[22]:
In [23]:
list(dates.date)
Out[23]:
In [24]:
dates.year
Out[24]:
In [25]:
list(dates.year)
Out[25]:
In [26]:
list(dates.day)
Out[26]:
In [27]:
str(dates.date)
Out[27]:
In [28]:
df=pd.DataFrame(np.random.randn(6,4),index=dates,columns=list("ABCD"))df
Out[28]:
In [29]:
df2 = pd.DataFrame({ 'A' : 1., 'B' : pd.Timestamp('20130102'), 'C' : pd.Series(1,index=list(range(4)),dtype='float32'), 'D' : np.array([3] * 4,dtype='int32'), 'E' : pd.Categorical(["test","train","test","train"]), 'F' : 'foo' }) #Creating a DataFrame by passing a dict of objects that can be converted to series-like.df2
Out[29]:
In [30]:
df2.dtypes
Out[30]:
In [31]:
df.dtypes
Out[31]:
In [32]:
df2.<TAB> #使用jupyter时按tab键,可以看到代码提示。
In [36]:
df.head()
Out[36]:
In [37]:
df.index
Out[37]:
In [38]:
df.columns
Out[38]:
In [39]:
df.values
Out[39]:
In [40]:
df.describe()
Out[40]:
In [41]:
df
Out[41]:
In [42]:
df.T
Out[42]:
In [43]:
df.sort_index(axis=1,ascending=False) #Sorting by an axis 排序。
Out[43]:
In [44]:
df.sort_values(by="B") #Sorting by values
Out[44]:
In [45]:
df
Out[45]:
In [46]:
df["A"]# Selecting a single column, which yields a Series, equivalent to df.A
Out[46]:
In [47]:
df.A
Out[47]:
In [48]:
df[0:3] #Selecting via [], which slices the rows.
Out[48]:
In [49]:
df["2017-01-13":"2017-01-17"]
Out[49]:
In [50]:
dates
Out[50]:
In [51]:
df.loc[dates[0]] #For getting a cross section using a label
Out[51]:
In [52]:
df.loc[:,["A","B"]]
Out[52]:
In [53]:
df.loc['20170112':'20170116',['A','B']] #Showing label slicing, both endpoints are included
Out[53]:
In [54]:
df.loc["20170115",["A","B"]]
Out[54]:
In [55]:
df.loc[dates[3],"D"] #For getting a scalar value
Out[55]:
In [56]:
df.at[dates[3],"D"] #For getting fast access to a scalar (equiv to the prior method)
Out[56]:
In [57]:
df.iloc[3] #Select via the position of the passed integers
Out[57]:
In [58]:
df.iloc[2:5,0:2] # By integer slices, acting similar to numpy/python
Out[58]:
In [59]:
df.iloc[[1,3,4],[0,2]] #By lists of integer position locations, similar to the numpy/python style
Out[59]:
In [60]:
df.iloc[1:3,:]
Out[60]:
In [61]:
df.iloc[:,1:3]
Out[61]:
In [62]:
df.iloc[1,1] #For getting a value explicitly
Out[62]:
In [63]:
df.iat[1,1] #For getting fast access to a scalar (equiv to the prior method)
Out[63]:
In [64]:
df[df.A>0] #Using a single column’s values to select data
Out[64]:
In [65]:
df[df>0] #Selecting values from a DataFrame where a boolean condition is met
Out[65]:
In [66]:
df2
Out[66]:
In [67]:
df
Out[67]:
In [68]:
df2=df.copy()df2
Out[68]:
In [69]:
df.equals(df2)
Out[69]:
In [70]:
df==df2
Out[70]:
In [71]:
df is df2
Out[71]:
In [72]:
df2["E"]=["one","one","two","three","four","three"]df2
Out[72]:
In [73]:
df2[df2.E.isin(["two","four"])]
Out[73]:
In [74]:
df2[df2["E"].isin(["two","four"])]
Out[74]:
In [75]:
s1= pd.Series([1,2,3,4,5,6],index=pd.date_range("20171016",periods=6)) #Setting a new column automatically aligns the data by the indexess1
Out[75]:
In [76]:
df.at[dates[0],"A"]=0 #Setting values by label
In [77]:
df
Out[77]:
In [78]:
df.iat[0,1]=0df
Out[78]:
In [79]:
df.loc[:,"D"]=np.array([5]*len(df)) #Setting by assigning with a numpy arraydf
Out[79]:
In [80]:
df2=df.copy()df2
Out[80]:
In [81]:
df2[df2>0]=-df2df2
Out[81]:
In [83]:
df
Out[83]:
In [84]:
df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E'])df1.loc[dates[0]:dates[1],'E'] = 1df1
Out[84]:
In [85]:
df1.dropna(how="any") #To drop any rows that have missing data
Out[85]:
In [86]:
df1.fillna(value=5) # Filling missing data
Out[86]:
In [87]:
df1
Out[87]:
In [88]:
pd.isnull(df1)
Out[88]:
In [89]:
df1.isnull()
Out[89]:
In [90]:
df1.isna() #没有这个方法~~
In [91]:
df
Out[91]:
In [92]:
df.mean()
Out[92]:
In [93]:
df.mean(1) #Same operation on the other axis
Out[93]:
In [94]:
s= pd.Series([1,2,3,np.nan,4,5],index=dates).shift(2) # Operating with objects that have different dimensionality and need alignment. In addition, pandas automatically broadcasts along the specified dimension.s
Out[94]:
In [95]:
df
Out[95]:
In [96]:
df.sub(s,axis="index") #dataFrame与series的减法
Out[96]:
In [97]:
df
Out[97]:
In [98]:
df.apply(np.cumsum) #行叠加。
Out[98]:
In [99]:
df.apply(lambda x: x.max()-x.min())
Out[99]:
In [100]:
s= pd.Series(np.random.randint(0,7,size=10))s
Out[100]:
In [101]:
s.value_counts()
Out[101]:
In [102]:
s= pd.Series(['A', 'B', 'C', 'Aaba', 'Baca', np.nan, 'CABA', 'dog', 'cat'])s.str.lower()
Out[102]:
In [103]:
s
Out[103]:
In [104]:
df
Out[104]:
In [105]:
df=pd.DataFrame(np.random.randn(10,4))df
Out[105]:
In [106]:
# break it into piecespieces=[df[:3],df[3:7],df[7:]]pd.concat(pieces)
Out[106]:
In [107]:
pieces
Out[107]:
In [108]:
left=pd.DataFrame({"key":["foo","foo"],"lval":[1,2]})right = pd.DataFrame({'key': ['foo', 'foo'], 'rval': [4, 5]})
In [109]:
left
Out[109]:
In [110]:
right
Out[110]:
In [111]:
pd.merge(left,right,on="key")
Out[111]:
In [112]:
left = pd.DataFrame({'key': ['foo', 'bar'], 'lval': [1, 2]})right = pd.DataFrame({'key': ['foo', 'bar'], 'rval': [4, 5]})
In [113]:
left
Out[113]:
In [114]:
right
Out[114]:
In [115]:
pd.merge(left,right,on="key")
Out[115]:
In [116]:
df = pd.DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])df
Out[116]:
In [117]:
s=df.iloc[3]s
Out[117]:
In [118]:
df.append(s,ignore_index=True)
Out[118]:
In [119]:
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
Out[119]:
In [120]:
df.groupby("A").sum()
Out[120]:
In [121]:
df.groupby(["A","B"]).sum() #Grouping by multiple columns forms a hierarchical index, which we then apply the function.
Out[121]:
In [122]:
tuples = list(zip([['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]))tuples
Out[122]:
In [123]:
tuples = list(zip(*[['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]))tuples
Out[123]:
In [124]:
index=pd.MultiIndex.from_tuples(tuples,names=["first","second"])index
Out[124]:
In [125]:
df=pd.DataFrame(np.random.randn(8,2),index=index,columns=['A', 'B'])df
Out[125]:
In [126]:
df2=df[:4]df2
Out[126]:
In [127]:
stacked= df2.stack()stacked
Out[127]:
In [128]:
stacked.unstack()
Out[128]:
In [129]:
stacked.unstack(1)
Out[129]:
In [130]:
stacked.unstack(0)
Out[130]:
In [131]:
df = pd.DataFrame({'A' : ['one', 'one', 'two', 'three'] * 3, 'B' : ['A', 'B', 'C'] * 4, 'C' : ['foo', 'foo', 'foo', 'bar', 'bar', 'bar'] * 2, 'D' : np.random.randn(12), 'E' : np.random.randn(12)})df
Out[131]:
In [4]:
pd.pivot_table(df,values="D",index=["A","B"],columns=["C"])
Out[4]:
In [132]:
rng=pd.date_range("1/2/2017",periods=100,freq="S")rng
Out[132]:
In [133]:
ts =pd.Series(np.random.randint(0,500,len(rng)),index=rng)ts
Out[133]:
In [7]:
ts.resample("5Min").sum()
Out[7]:
In [9]:
ts.resample("1Min").sum()
Out[9]:
In [10]:
rng= pd.date_range("2/1/2017 00:00",periods=5,freq="D")rng
Out[10]:
In [12]:
ts=pd.Series(np.random.randn(len(rng)),index=rng)ts
Out[12]:
In [13]:
tsUtc=ts.tz_localize("UTC")tsUtc
Out[13]:
In [14]:
tsUtc.tz_convert("US/Eastern")
Out[14]:
In [15]:
tsUtc
Out[15]:
In [16]:
rng=pd.date_range("1/8/2017",periods=5,freq="M")rng
Out[16]:
In [18]:
ts=pd.Series(np.random.randn(len(rng)),rng)ts
Out[18]:
In [20]:
ps=ts.to_period()ps
Out[20]:
In [21]:
ps.to_timestamp()
Out[21]:
In [22]:
ps
Out[22]:
In [23]:
prng=pd.period_range("1990Q1","2017Q4",freq="Q-NOV")prng
Out[23]:
In [25]:
ts= pd.Series(np.random.randn(len(prng)),prng)ts.head()
Out[25]:
In [26]:
ts.index=(prng.asfreq("M","e")+1).asfreq("H","s")+9ts.head()
Out[26]:
In [34]:
df = pd.DataFrame({"id":[1,2,3,4,5,6],"raw_grade":["a","a","c","b","b","f"]})df
Out[34]:
In [35]:
df["grade"]=df.raw_grade.astype("category")df
Out[35]:
In [36]:
df.grade #Convert the raw grades to a categorical data type
Out[36]:
In [37]:
# Rename the categories to more meaningful names (assigning to Series.cat.categories is inplace!)df.grade.cat.categories=["very good","good","nomal","bad"]df
Out[37]:
In [38]:
# Reorder the categories and simultaneously add the missing categories (methods under Series .cat return a new Series per default).df.grade=df.grade.cat.set_categories(["very bad", "bad", "medium","good", "very good"])df.grade
Out[38]:
In [39]:
df
Out[39]:
In [40]:
df.sort_values(by="grade")
Out[40]:
In [41]:
df.groupby("grade").size()
Out[41]:
In [43]:
ts=pd.Series(np.random.randn(1000),index=pd.date_range("1/1/2017",periods=1000))ts.head()
Out[43]:
In [45]:
ts=ts.cumsum()ts.head()
Out[45]:
In [48]:
ts.plot()
Out[48]:
In [50]:
df=pd.DataFrame(np.random.randn(1000,4),index=ts.index,columns=["A","B","C","D"])df.head()
Out[50]:
In [51]:
df=df.cumsum()
In [53]:
plt.figure()df.plot()plt.legend(loc="best")plt.show()
In [ ]:
df.to_csv("foo.csv")
In [ ]:
pd.read_csv("foo.csv")
In [ ]:
df.to_hdf("foo.h5","df")
In [ ]:
pd.read_hdf("foo.h5","df")
In [ ]:
df.to_excel('foo.xlsx', sheet_name='Sheet1')
In [ ]:
pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
阅读全文
0 0
- Pandas 10分钟入门(官方说明+个人小测试)
- 10分钟入门pandas
- Pandas 十分钟入门
- 【翻译】Pandas 十分钟入门
- 十分钟掌握pandas(pandas官方文档翻译)
- 10分钟搞定pandas
- 10分钟学会pandas
- 10分钟Pandas教程
- [Pandas] 10分钟Pandas之旅 01
- [Pandas] 10分钟Pandas之旅 02
- 十分钟学会pandas(入门级)
- python之10分钟pandas
- 10分钟快速了解Pandas
- Pandas-office-10分钟开始
- 十分钟搞定pandas(官方学习文档的译文)
- 个人小测试
- 10 MINUTES TO PANDAS(10分钟搞定Pandas)
- 10 Minutes to pandas----十分钟搞定Pandas
- 图论——马语翻译
- 视频:行业首款智能手表跟拍与避障无人机XEagle发布会
- 移动端车牌字符识别技术
- Conditional Variable
- Windows7查看本地Java安装是否成功和路径的方法
- Pandas 10分钟入门(官方说明+个人小测试)
- 恩节教父母get新技能:一键同步手机联系人
- 无限极携手众星掀起“让我养你吧”情话营销热潮
- 马云现身芒果TV 对极米无屏电视赞不绝口
- 机器学习扫盲---常见学习效果指标说明(并附基于sklearn库计算AUC)
- 开放Cortex-M0处理器,ARM让人人都能设计芯片
- linux基础(二)
- 远程访问Jupyter Notebook应用(Linux虚拟机)
- sqoop使用