1. Pandas數據構成
1)Series數據構成
>>>import pandas as pd
>>>series1 = pd.Series([1, 2, 3])
>>>series1
0 1
1 2
2 3
2)DataFrame數據構成
>>>import pandas as pd
>>>import numpy as np
>>>dataframe1 = pd.DataFrame(np.arange(12).reshape(3, 4))
0 1 2 3
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
>>>dataframe1 = pd.DataFrame(np.arange(12).reshape(3, 4), index = ['row0', 'row1', 'row2'], columns = ['col0', 'col1', 'col2', 'col3'])
>>>datafram1
col0 col1 col2 col3
row0 0 1 2 3
row1 4 5 6 7
row2 8 9 10 11
>>>datafram1 = pd.DataFrame({'col0':0, 'col1':1, 'col2':[2, 2, 2]})
>>>datafram1
col0 col1 col2
0 0 1 2
1 0 1 2
2 0 1 2
2. Pandas數據的索引
>>>import pandas as pd
>>>import numpy as np
#Series的索引
>>>series1 = pd.Series([1, 2, 3, 4, 5, 6], index = ['A', 'B', 'C', 'D', 'E', 'F'])
>>>series1
A 1
B 2
C 3
D 4
E 5
F 6
>>>series1['A']
1
>>>series1[0]
1
#DataFrame的索引
>>>dataframe1 = pd.DataFrame(np.arange(12).reshape(3, 4), index = ['row0', 'row1', 'row2'], columns = ['col0', 'col1', 'col2', 'col3'])
>>>dataframe1
col0 col1 col2 col3
row0 0 1 2 3
row1 4 5 6 7
row2 8 9 10 11
>>>dataframe1.col0
row0 0
row1 4
row2 8
>>>dataframe1['col0']
row0 0
row1 4
row2 8
#按標簽進行索引
>>>dataframe1.loc['row0']
col0 0
col1 1
col2 2
col3 3
>>>dataframe1.loc['row0', 'col0']
0
#按位置使用序號索引
>>>dataframe1.iloc[0]
col0 0
col1 1
col2 2
col3 3
>>>dataframe1.iloc[0, 1]
1
#混合索引,不建議使用
>>>dataframe1.ix['row0', 0]
0
>>>dataframe1.ix[0, 'col0']
0
3. Pandas數據的插入和修改
>>>import numpy as np
>>>import pandas as pd
>>>dataframe1 = pd.DataFrame(np.arange(12).reshape(3, 4), index = ['row0', 'row1', 'row2'], columns = ['col0', 'col1', 'col2', 'col3'

本文介绍了Python中Pandas模块的基础操作,包括Series和DataFrame的数据构成,数据索引,插入和修改数据的方法,处理nan值的策略,以及DataFrame的合并操作,最后探讨了Pandas的图表绘制功能。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



