首页 > 编程知识 正文

pandas基础知识,python入门教程(非常详细)

时间:2023-05-03 05:07:41 阅读:120977 作者:4445

Pandas用法importpandasaspdimportnumpyasnp1.创建Series 1(空Series s=pd.Series ) )创建sipython-input-3-85850638 a 11438 faultdtypeforemptyserieswillbe ' object ' instead of version.specifyadtypeexplicitlytosilencethiswarning.s=PD.series () 从dtype: float64 )2)创建nd array 103 ) ARR=NP.Array(['a '、' b '、' c '、' d'] ) ser1=PD.series(ARR ' b':11,' c':22,' d':3}dic {'a': 0,' b':' c':2,' d ' :3 } 在这种情况下,索引的长度s=pd.Series(5) 5, 必须指定索引以匹配3 ) s051 52 53 5d类型: int 642.从特定位置的Series访问数据(pandas片)1) Series的第一个元素s=PD.series ([ 索引=' e ' ) s a1b2c3 D4 e 5d type : int 64 s [0]1)2) Series的前三个元素s [ :3 ] a1b 2c 3d type : int 643) Series的后三个元素s C3 D4 e 5d type : int 64 s [-3: ] C3 D4 e 5d type : int 643 .使用标签搜索数据(索引) :一个Series就像一个固定大小的词典。 使用索引标签搜索单个元素s=PD.serieeees (可以通过索引标签获取和设置值)的3,4,5 ],index=['a ',' b ',' c ',' d ', ' e'] ) s a1b2c3 D4 e 5d类型: int 64 s [ ' b ' ]2)使用索引标签列表查找多个元素S1=PD.' e ' ] S1 a 1b2c3 D4 e 5d类型: int 64 protype ' d'] 3)如果不包含标记,则搜索时会出现异常---------------------程序数据 anaconda3 libsition 方法公差(2894 try :-2895 return self._ engine.get _ loc ) casted_key ) 2896 exceptkeyerroraserr : pandas . index.index engine.get _ loc (pandas (libs ) index.pyxinpandas._ libs.Inde ex hashtable _ class _ helper.pxiiindas hashtable _ class _ helper.pxiinpandas._ libs.hashtable.pyobjecthashtable.get _ item (key error 360 ' f ' theback (most recent call last)<ipython-input-24-c23937ec966b> in <module>----> 1 s['f']C:ProgramDataAnaconda3libsite-packagespandascoreseries.py in __getitem__(self, key) 880 881 elif key_is_scalar:--> 882 return self._get_value(key) 883 884 if is_hashable(key):C:ProgramDataAnaconda3libsite-packagespandascoreseries.py in _get_value(self, label, takeable) 987 988 # Similar to Index.get_value, but we do not fall back to positional--> 989 loc = self.index.get_loc(label) 990 return self.index._get_values_for_loc(self, loc, label) 991 C:ProgramDataAnaconda3libsite-packagespandascoreindexesbase.py in get_loc(self, key, method, tolerance) 2895 return self._engine.get_loc(casted_key) 2896 except KeyError as err:-> 2897 raise KeyError(key) from err 2898 2899 if tolerance is not None:KeyError: 'f' 4.简单运算

在pandas的Series中,会保留NumPy的数组操作(用布尔数组过滤数据,标量乘法,以及使用数学函数),并同时保持引用的使用

1) 例 ser2 = pd.Series(range(4),index = ["a","b","c","d"]) ser2 a 0b 1c 2d 3dtype: int64 ser2[ser2 > 2] d 3dtype: int64 ser2 * 2 a 0b 2c 4d 6dtype: int64 np.exp(ser2) a 1.000000b 2.718282c 7.389056d 20.085537dtype: float64

Series的自动对齐Series的一个重要功能就是自动对齐(不明觉厉),看看例子就明白了。 差不多就是不同Series对象运算的时候根据其索引进行匹配计算

1)创建两个Series名为ser3与ser4. sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000} sdata {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000} ser3 = pd.Series(sdata) ser3 Ohio 35000Texas 71000Oregon 16000Utah 5000dtype: int64 states = ['California', 'Ohio', 'Oregon', 'Texas'] states ['California', 'Ohio', 'Oregon', 'Texas'] ser4 = pd.Series(sdata,index = states) ser4 California NaNOhio 35000.0Oregon 16000.0Texas 71000.0dtype: float64 ser3+ser4 California NaNOhio 70000.0Oregon 32000.0Texas 142000.0Utah NaNdtype: float64 5.Series增删改 1) 增:Series的add()方法是加法计算不是增加Series元素用的,使用append连接其他Series。 sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000} sdata {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000} ser3 = pd.Series(sdata) ser3 Ohio 35000Texas 71000Oregon 16000Utah 5000dtype: int64 states = ['California', 'Ohio', 'Oregon', 'Texas'] states ['California', 'Ohio', 'Oregon', 'Texas'] ser4 = pd.Series(sdata,index = states) ser4 California NaNOhio 35000.0Oregon 16000.0Texas 71000.0dtype: float64 ser3.append(ser4) Ohio 35000.0Texas 71000.0Oregon 16000.0Utah 5000.0California NaNOhio 35000.0Oregon 16000.0Texas 71000.0dtype: float64 2)删:Series的drop()方法可以对Series进行删除操作,返回一个被删除后的Series,原来的Series不改变 s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) s a 1b 2c 3d 4e 5dtype: int64 a1 = s.drop('a') s a 1b 2c 3d 4e 5dtype: int64 a1 b 2c 3d 4e 5dtype: int64 3) 改:通过索引的方式查找到某个元素,然后通过“=”赋予新的值 s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e']) s a 1b 2c 3d 4e 5dtype: int64 s['a']=5 s a 5b 2c 3d 4e 5dtype: int64

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。