Pandas基础
一、Series
Series讲解
from pandas import Series,DataFrame
import pandas as pd
obj = Series([1, -2, 3, -4])
type(obj)
obj2 = Series([1, -2, 3, -4], index=['a', 'b', 'c', 'd'])
obj2.values
obj2.index
obj2[2]
obj2[['c']]
obj2['c'] = 23
obj2[obj2 < 0 ]
obj2 * 2
np.abs(obj2)
data = {
'张三':92,
'李四':78,
'王五':68,
'小明':82
}
obj3 = Series(data)
obj4 = Series(data, index=names)
obj4.name = 'math'
obj4.index.name = 'students'
二、DataFrame
import numpy as np
from pandas import Series,DataFrame
import pandas as pd
data = {
'name':['张三', '李四', '王五', '小明'],
'sex':['female', 'female', 'male', 'male'],
'year':[2001, 2001, 2003, 2002],
'city':['北京', '上海', '广州', '北京']
}
df = DataFrame(data)
df = DataFrame(data, columns=['name', 'sex', 'year', 'city'])
df = DataFrame(data, columns=['name', 'sex', 'year', 'city'],index=['a', 'b', 'c', 'd'])
'sex' in df.columns
'f' in df.index
三、索引值
obj = Series([1, -2, 3, -4], index=['b', 'a', 'c', 'd'])
obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])
obj2 = obj.reindex(range(6),method='ffill')
df2 = df.reindex(['a', 'b', 'c', 'd'],fill_value=0.0)
df3 = df2.reset_index(drop=True)
df2 = df.set_index('name')
四、索引和选取
obj[['a','c']]
obj['a':'c']
df[['city','sex']]
df2.loc['张三']
df2.iloc[1]
df2[(df2['sex'