导图社区 Python数据科学编程-2.panda基本操作
df.reindex([1990, 1991, 1992,2001, 2002, 2003], columns=['Chengdu', 'Shenzhen', 'Beijing', 'Lanzhou'])。
社区模板帮助中心,点此进入>>
英语词性
互联网9大思维
组织架构-单商户商城webAPP 思维导图。
法理
刑法总则
【华政插班生】文学常识-先秦
【华政插班生】文学常识-秦汉
文学常识:魏晋南北朝
【华政插班生】文学常识-隋唐五代
【华政插班生】文学常识-两宋
panda基本操作
Series
创建
obj2 = pd.Series(np.random.randn(5), index = ['a', 'b', 'c', 'd', 'end'])
设置索引
In [26]: dd = {'Beijing': 123, 'Shanghai': 456, 'Lanzhou': 789, 'Shenzhen': 1011} In [27]: pops = pd.Series(dd, index=['Beijing', 'Shanghai', 'Lanzhou', 'Hangzhou'])
查看索引和值
obj.index
obj.values
重设索引:Indexes can be re-assigned
pops.index = ['Recife', 'Salvador', 'Rio de Janeiro', 'Brasilia']
设置name:Series and their indexes can have names
分片:Slicing works, but it's peculiar with int indexes
pops.loc["Shanghai":]
In [124]: s = pd.Series(['a', 'b', 'c', 'd', 'e'], index=[1,2,3,67,9]) In [125]: s.loc[1:9]
DF
创建DF
创建:Creating a DataFrame from a dictionary
frame = pd.DataFrame(data, columns=['year', 'pop', 'city'])
Creating a DataFrame from a dictionary of dictionaries
theDict = {'Beijing': {2001: 2.4, 2002: 3.1}, 'Lanzhou':{2002:5., 2003:1.7}}
重设索引
df.reindex([1990, 1991, 1992,2001, 2002, 2003], columns=['Chengdu', 'Shenzhen', 'Beijing', 'Lanzhou'])
删除行/列
frame.drop(['Japan', 'Argentina'],axis)
转置:DataFrames can also be tranposed
索引小操作:Some basic operations with Indexes
df.index.union(df.columns) df.index.intersection(df.columns)
进阶操作
整行/列操作:The incredibly useful apply() method
df2.apply(lambda x: x.max() + x.min(), axis=1)
排序:Sorting the index
df1.sort_index(ascending=False, axis=1)
排行:Ranking values
df1.rank()
Uniqueness of Series elements
anotherSeries.isin(mySeries)
合并Series
Adding up two DataFrames with add()
ser + ser2
ser.add(ser2, fill_value=0)
Adding a DataFrame and a Series
The Series is broadcasted to all the DataFrame columns
筛选
year_sums[year_sums == year_sums.max()]
unique和(⭐)value-count
mySeries.unique() mySeries.value_counts()
索引并不copy:Indexing produces visualisations, not copies