series的使用
import pandas as pd
import string
t = pd.Series([1,2,31,23,53,5])
print(t)
type(t)
# 指定索引值
t1 = pd.Series ([1,2,3,4],index=['a','b','c','d'])
t2 = pd.Series ([1,2,3,4],list('abcd'))
print('t1')
print(t1)
print('t2')
print(t2)
# 字典变为Series
temp_dict = {"name":"meili","age":18,"tel":10086}
t3 = pd.Series(temp_dict)
print('t3')
print(t3)
c = {string.ascii_uppercase[i]:i for i in range(5)}
t4 = pd.Series(c)
print('t4')
print(t4)
# 切片和索引
print(t3["age"])
print(t3[1])
print(t3[:2])
print(t3.index)
for i in t3.index:
print(i)
print(list(t3.index)[:2])
DataFrame
import pandas as pd
d1 = {'name':['Meili','Shuaiqi'],"age":[18,21],'tel':[10086,10000]}
t1 = pd.DataFrame(d1)
print('t1')
print(t1)
d2 = [{'name':'Xiaohong','age':18,'tel':10086},{'name':'Xiaognag','tel':10088},{'name':"Dacongm",'age':23}]
t2 = pd.DataFrame(d2)
print('t2')
print(t2)
读取外部数据
import pandas as pd
df = pd.read_csv('./dogNames2.csv')
# print(df.head())
# print(df.info())
dff = df.sort_values(by='Count_AnimalName',ascending=False)
# print(dff)
# print(dff.head(5))
print(dff[:20])
print('Row_Labels')
print(dff['Row_Labels'])
import pandas as pd
import numpy as np
file_path = 'IMDB-Movie-Data.csv'
df = pd.read_csv(file_path)
print(df.info())
print(df.head(1))
print('Rating')
print(df['Rating'].mean())
print('Director')
print(len(set(df['Director'].tolist())))
print(len(df['Director'].unique()))
print('actor')
temp_actor_list = df['Actors'].str.split(',').tolist()
actor_list = [i for j in temp_actor_list for i in j]
actor_num = len(set(actor_list))
print(actor_num)
索引和切片
import pandas as pd
import numpy as np
t1 = pd.DataFrame(np.arange(12).reshape(3,4),index=list("abc"),columns=list("DEFG"))
print('t1')
print(t1)
print("*"*50)
print(t1.loc["a","E"])
print(t1.loc["a",['D',"G"]])
print(t1.loc["a":"c",["D","F","G"]])
print(t1.loc["a",:])
print(t1.loc[:,"D"])
print(t1.loc["a":,"G"])
print(t1.loc["a",:"G"])
print("*"*50)
print(t1.iloc[1,2])
print(t1.iloc[2:,1:])
print(t1.iloc[[1,2],[1,3]])
缺失数据的处理
import pandas as pd
import numpy as np
t1 = pd.DataFrame(np.arange(12).reshape(3,4),index=list('abc'),columns=list('WXYZ'))
t1.loc['a','W'] = np.nan
t1.loc['c','Z'] = np.nan
print ('t1')
print(t1)
print(pd.isnull(t1))
print(pd.notnull(t1))
print(pd.notnull(t1['W']))
t11 = t1[pd.notnull(t1['W'])]
print('t11')
print(t11)
t12 = t1.dropna(axis=0,how='any',inplace=False)
print('t12')
print(t12)
t13 = t1.dropna(axis=0,how='all',inplace=False)
print('t13')
print(t13)
t14 = t1.fillna(t1.mean())
print('t14')
print(t14)
d2 = [{'name':'Xiaohong','age':18,'tel':10086},{'name':'Xiaognag','tel':10088},{'name':"Dacongm",'age':23}]
t2 = pd.DataFrame(d2)
t21 = t2.fillna(t2.mean())
print('t21')
print(t21)
t2['age'] = t2['age'].fillna(t2['age'].mean())
print('t2')
print(t2)
|