该套笔记基于 https://www.youtube.com/watch?v=yzIMircGU5I&list=PL5-da3qGB5ICCsgW1MxlZ0Hq8LL5U3u9y&index=1
或者墙内B站有https://www.bilibili.com/video/av6785636
一. What is Pandas(略)
二. How do I read a tabular data file into pandas
# coding: utf-8
# In[6]:
import pandas as pd
user_cols = ['user_id','age','gender','occupation','zip_code']
users = pd.read_table('http://bit.ly/movieusers',sep='|',header = None,names = user_cols)
users.head()
# In[7]:
pd.read_table('http://bit.ly/chiporders')
三. How do I select a Pandas series from a Data Frame
# coding: utf-8
# In[8]:
import pandas as pd
# In[9]:
ufo = pd.read_csv('http://bit.ly/uforeports')
# In[10]:
type(ufo)
# In[11]:
ufo.head()
# In[13]:
ufo.shape
# In[15]:
type(ufo['City'])
# In[16]:
ufo['Colors Reported']
# In[17]:
'ab'+'cd'
# In[22]:
#If we want to add a new column, we must use bracket.
ufo['Location'] = ufo.City + ', ' + ufo.State
# In[23]:
ufo.head()
四. Why do some Pandas commands end with parenthesis
# coding: utf-8
# In[24]:
import pandas as pd
# In[27]:
movies = pd.read_csv('http://bit.ly/imdbratings')
# In[28]:
movies.head()
# In[29]:
movies.describe()
# In[30]:
movies.shape
# In[31]:
movies.dtypes
# In[32]:
type(movies)
# In[34]:
movies.describe(include = ['object'])
#任何时候,在光标在某个位置上时,敲击Shift + Tab,可以看到这个函数的相关信息
五. How do I rename columns in a Pandas Data Frame
# coding: utf-8
# In[35]:
import pandas as pd
# In[36]:
ufo = pd.read_csv('http://bit.ly/uforeports')
# In[37]:
ufo.head()
# In[38]:
ufo.columns
# In[41]:
ufo.rename(columns = {'Colors Reported':'Colors_Reported', 'Shape Reported':'Shape_Reported'}, inplace = True)
# In[42]:
ufo.columns
# In[45]:
ufo_cols = ['city', 'colors reported', 'shape reported', 'state', 'time']
# In[48]:
ufo.columns = ufo_cols
# In[49]:
ufo.head()
# In[50]:
ufo = pd.read_csv('http://bit.ly/uforeports',names = ufo_cols, header = 0)
# In[51]:
ufo.head()
# In[52]:
#special way for replacing names
ufo.columns = ufo.columns.str.replace(' ','_')
# In[53]:
ufo.columns
六. How do I remove columns from a Pandas DataFrame
# coding: utf-8
# In[67]:
import pandas as pd
# In[68]:
ufo = pd.read_csv('http://bit.ly/uforeports')
# In[69]:
ufo.head()
# In[70]:
ufo.shape
# In[71]:
ufo.drop('Colors Reported',axis = 1,inplace =True)
# In[72]:
ufo.head()
# In[73]:
#axis = 1, 表示drop的是Column
ufo.drop(['City','State'], axis = 1, inplace = True)
# In[74]:
ufo.head()
# In[75]:
#axis = 0, 表示drop的是row
ufo.drop([0,1], axis = 0, inplace = True)
# In[76]:
ufo.head()