Python中Pandas模塊的基本操作

本文介绍了Python中Pandas模块的基础操作,包括Series和DataFrame的数据构成,数据索引,插入和修改数据的方法,处理nan值的策略,以及DataFrame的合并操作,最后探讨了Pandas的图表绘制功能。
摘要由CSDN通过智能技术生成

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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值