pandas学习(长期更新)

在这里插入图片描述
iloc方法得到pandas序列求和是浮点数

重命名列,merge方法:

(base) xutengfei@xutengfei-G3-3579:~$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd 
>>> df1=pd.DataFrame(np.arange(9).reshape((3,3)),columns=list("hey"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'np' is not defined
>>> import numpy as np
>>> df1=pd.DataFrame(np.arange(9).reshape((3,3)),columns=list("hey"))
>>> df1
   h  e  y
0  0  1  2
1  3  4  5
2  6  7  8
>>> df2=pd.DataFrame(np.ones(2,4),index=['E','F'],columns=list("hold"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xutengfei/.local/lib/python3.8/site-packages/numpy/core/numeric.py", line 192, in ones
    a = empty(shape, dtype, order)
TypeError: Cannot interpret '4' as a data type
>>> df2=pd.DataFrame(np.ones(2,4),index=['E','F'],columns=list("hold"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xutengfei/.local/lib/python3.8/site-packages/numpy/core/numeric.py", line 192, in ones
    a = empty(shape, dtype, order)
TypeError: Cannot interpret '4' as a data type
>>> df2=pd.DataFrame(np.ones(2,4),index=['E','F'],columns=list("hold"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xutengfei/.local/lib/python3.8/site-packages/numpy/core/numeric.py", line 192, in ones
    a = empty(shape, dtype, order)
TypeError: Cannot interpret '4' as a data type
>>> df2=pd.DataFrame(np.ones((2,4)),index=['E','F'],columns=list("hold"))
>>> df2
     h    o    l    d
E  1.0  1.0  1.0  1.0
F  1.0  1.0  1.0  1.0
>>> df2.merge(df1,on="h")
Empty DataFrame
Columns: [h, o, l, d, e, y]
Index: []
>>> df2
     h    o    l    d
E  1.0  1.0  1.0  1.0
F  1.0  1.0  1.0  1.0
>>> df2.rename({
    
    'h':'e'},inplace=True)
>>> df2
     h    o    l    d
E  1.0  1.0  1.0  1.0
F  1.0  1.0  1.0  1.0
>>>  df2.rename(columns={
    
    'h':'e'},inplace=True)
  File "<stdin>", line 1
    df2.rename(columns={
    
    'h':'e'},inplace=True)
    ^
IndentationError: unexpected indent
>>> df2.rename(columns={
    
    'h':'e'},inplace=True)
>>> df2
     e    o    l    d
E  1.0  1.0  1.0  1.0
F  1.0  1.0  1.0  1.0
>>> df2.merge(df1,on="3")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/xutengfei/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py", line 7946, in merge
    return merge(
  File "/home/xutengfei/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 74, in merge
    op = _MergeOperation(
  File "/home/xutengfei/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 652, in __init__
    ) = self._get_merge_keys()
  File "/home/xutengfei/anaconda3/lib/python3.8/site-packages/pandas/core/reshape/merge.py", line 1005, in _get_merge_keys
    right_keys.append(right._get_label_or_level_values(rk))
  File "/home/xutengfei/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py", line 1563, in _get_label_or_level_values
    raise KeyError(key)
KeyError: '3'
>>> df2.merge(df1,on="e")
     e    o    l    d  h  y
0  1.0  1.0  1.0  1.0  0  2
1  1.0  1.0  1.0  1.0  0  2
>>> 


利用字典和列表创建dataframe的方法:

1.直接传入一个字典-键值对(键:各个列,值:各个列的行值,以列表呈现)
2.传入一个列表,列表中的每个元素是字典,记录每行的内容。\

如何避免人为0的影响:

df[df==0]=np.nan
如此一来,在计算平均值时,就可以避免人为0的影响。

猜你喜欢

转载自blog.csdn.net/qq_44065334/article/details/113370909