当前位置:   article > 正文

Python pandas中DataFrame添加列、获取行列、获取元素值_dataframe增加列和相应的值

dataframe增加列和相应的值

一、DataFrame添加列

直接通过赋值为空,添加一列。

  1. >>> import pandas as pd
  2. >>> df = pd.DataFrame(np.arange(12).reshape(3, 4), index = ['row1', 'row2', 'row3'], columns=['col1', 'col2', 'col3', 'col4'])
  3. >>> df
  4. col1 col2 col3 col4
  5. row1 0 1 2 3
  6. row2 4 5 6 7
  7. row3 8 9 10 11
  8. >>>
  9. >>> df['col5']=''
  10. >>>
  11. >>> df
  12. col1 col2 col3 col4 col5
  13. row1 0 1 2 3
  14. row2 4 5 6 7
  15. row3 8 9 10 11

通过一个list给新加的列赋值,添加一列。注意,list里的元素个数要跟dataframe的行数一致,否则回报长度对不齐的错误。

  1. >>> df['col6']=[1,1,1]
  2. >>>
  3. >>> df
  4. col1 col2 col3 col4 col5 col6
  5. row1 0 1 2 3 1
  6. row2 4 5 6 7 1
  7. row3 8 9 10 11 1
  8. >>> df['col7']=[]
  9. Traceback (most recent call last):
  10. File "<stdin>", line 1, in <module>
  11. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3119, in __setitem__
  12. self._set_item(key, value)
  13. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3194, in _set_item
  14. value = self._sanitize_column(key, value)
  15. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3391, in _sanitize_column
  16. value = _sanitize_index(value, self.index, copy=False)
  17. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/series.py", line 4001, in _sanitize_index
  18. raise ValueError('Length of values does not match length of ' 'index')
  19. ValueError: Length of values does not match length of index
  20. >>>
  21. >>> df['col7']=[1]
  22. Traceback (most recent call last):
  23. File "<stdin>", line 1, in <module>
  24. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3119, in __setitem__
  25. self._set_item(key, value)
  26. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3194, in _set_item
  27. value = self._sanitize_column(key, value)
  28. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3391, in _sanitize_column
  29. value = _sanitize_index(value, self.index, copy=False)
  30. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/series.py", line 4001, in _sanitize_index
  31. raise ValueError('Length of values does not match length of ' 'index')
  32. ValueError: Length of values does not match length of index

如果需要在指定位置处添加一列,用insert方法实现。DataFrame.insert(loc, column, value, allow_duplicates=False),loc-位置,column-列名,value-元素值,allow_duplicates-是否允许重列。

  1. >>> df
  2. col1 col2 col3 col4 col5 col6
  3. row1 0 1 2 3 1
  4. row2 4 5 6 7 1
  5. row3 8 9 10 11 1
  6. >>>
  7. >>> df.insert(1,'col7','')
  8. >>>
  9. >>> df
  10. col1 col7 col2 col3 col4 col5 col6
  11. row1 0 1 2 3 1
  12. row2 4 5 6 7 1
  13. row3 8 9 10 11 1
  14. >>>
  15. >>> df.insert(4,'col8',[2,2,2])
  16. >>>
  17. >>> df
  18. col1 col7 col2 col3 col8 col4 col5 col6
  19. row1 0 1 2 2 3 1
  20. row2 4 5 6 2 7 1
  21. row3 8 9 10 2 11 1

二、DataFrame取行列

1、按索引或者行列名选取DataFrame的具体行和列

  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. >>>
  7. >>> col1=df['col1']
  8. >>>
  9. >>> col1
  10. row1 0
  11. row2 4
  12. row3 8
  13. Name: col1, dtype: int64
  14. >>>
  15. >>> type(col1)
  16. <class 'pandas.core.series.Series'>

注意,不能用行名取行,也不能用数字索引取行列,以下方式都会报错。

  1. >>> row1=df['row1']
  2. Traceback (most recent call last):
  3. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
  4. return self._engine.get_loc(key)
  5. File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  6. File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  7. File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  8. File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
  9. KeyError: 'row1'
  10. During handling of the above exception, another exception occurred:
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2688, in __getitem__
  14. return self._getitem_column(key)
  15. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
  16. return self._get_item_cache(key)
  17. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache
  18. values = self._data.get(item)
  19. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/internals.py", line 4115, in get
  20. loc = self.items.get_loc(item)
  21. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
  22. return self._engine.get_loc(self._maybe_cast_indexer(key))
  23. File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  24. File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  25. File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  26. File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
  27. KeyError: 'row1'
  28. >>>
  29. >>> row1=df[0]
  30. Traceback (most recent call last):
  31. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
  32. return self._engine.get_loc(key)
  33. File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  34. File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  35. File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  36. File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
  37. KeyError: 0
  38. During handling of the above exception, another exception occurred:
  39. Traceback (most recent call last):
  40. File "<stdin>", line 1, in <module>
  41. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2688, in __getitem__
  42. return self._getitem_column(key)
  43. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
  44. return self._get_item_cache(key)
  45. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 2489, in _get_item_cache
  46. values = self._data.get(item)
  47. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/internals.py", line 4115, in get
  48. loc = self.items.get_loc(item)
  49. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
  50. return self._engine.get_loc(self._maybe_cast_indexer(key))
  51. File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  52. File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  53. File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  54. File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
  55. KeyError: 0
  56. >>>
  57. >>> row1=df[0,:]
  58. Traceback (most recent call last):
  59. File "<stdin>", line 1, in <module>
  60. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2688, in __getitem__
  61. return self._getitem_column(key)
  62. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
  63. return self._get_item_cache(key)
  64. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 2487, in _get_item_cache
  65. res = cache.get(item)
  66. TypeError: unhashable type: 'slice'
  67. >>>
  68. >>> col1=df[:,0]
  69. Traceback (most recent call last):
  70. File "<stdin>", line 1, in <module>
  71. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2688, in __getitem__
  72. return self._getitem_column(key)
  73. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2695, in _getitem_column
  74. return self._get_item_cache(key)
  75. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 2487, in _get_item_cache
  76. res = cache.get(item)
  77. TypeError: unhashable type: 'slice'

取某些行可以用限定行号的方式。

  1. >>> row1=df[0:1]
  2. >>>
  3. >>> row1
  4. col1 col7 col2 col3 col8 col4 col5 col6
  5. row1 0 1 2 2 3 1
  6. >>>
  7. >>> row2=df[1:2]
  8. >>>
  9. >>> row2
  10. col1 col7 col2 col3 col8 col4 col5 col6
  11. row2 4 5 6 2 7 1
  12. >>>
  13. >>> row23=df[1:3]
  14. >>>
  15. >>> row23
  16. col1 col7 col2 col3 col8 col4 col5 col6
  17. row2 4 5 6 2 7 1
  18. row3 8 9 10 2 11 1

2、使用iloc按行号和列号取相应的行列

  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. # 取第一行
  7. >>> row1=df.iloc[0]
  8. >>>
  9. >>> row1
  10. col1 0
  11. col7
  12. col2 1
  13. col3 2
  14. col8 2
  15. col4 3
  16. col5
  17. col6 1
  18. Name: row1, dtype: object
  19. # 取第123
  20. >>> row123=df.iloc[[0,1,2]]
  21. >>>
  22. >>> row123
  23. col1 col7 col2 col3 col8 col4 col5 col6
  24. row1 0 1 2 2 3 1
  25. row2 4 5 6 2 7 1
  26. row3 8 9 10 2 11 1
  27. >>>
  28. >>> row12=df.iloc[[0,1]]
  29. >>>
  30. >>> row12
  31. col1 col7 col2 col3 col8 col4 col5 col6
  32. row1 0 1 2 2 3 1
  33. row2 4 5 6 2 7 1
  34. # 连续取多行
  35. >>> row12=df.iloc[0:2]
  36. >>>
  37. >>> row12
  38. col1 col7 col2 col3 col8 col4 col5 col6
  39. row1 0 1 2 2 3 1
  40. row2 4 5 6 2 7 1
  41. >>>
  42. >>> row12=df.iloc[0:2,:]
  43. >>>
  44. >>> row12
  45. col1 col7 col2 col3 col8 col4 col5 col6
  46. row1 0 1 2 2 3 1
  47. row2 4 5 6 2 7 1
  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. # 取行时可以不指定列,但取列时必须用:,来指定全行
  7. >>> col1=df.iloc[:,0]
  8. >>>
  9. >>> col1
  10. row1 0
  11. row2 4
  12. row3 8
  13. Name: col1, dtype: int64
  14. # 用list[0,2]指定取第13
  15. >>> col13=df.iloc[:,[0,2]]
  16. >>>
  17. >>> col13
  18. col1 col2
  19. row1 0 1
  20. row2 4 5
  21. row3 8 9
  22. >>>
  23. >>> col123=df.iloc[:,[0,1,2]]
  24. >>>
  25. >>> col123
  26. col1 col7 col2
  27. row1 0 1
  28. row2 4 5
  29. row3 8 9
  30. # 用0:2指定连续的多列
  31. >>> col12=df.iloc[:,0:2]
  32. >>>
  33. >>> col12
  34. col1 col7
  35. row1 0
  36. row2 4
  37. row3 8

3、使用loc按行名和列名取相应的行列

  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. >>>
  7. >>> row1=df.loc['row1']
  8. >>>
  9. >>> row1
  10. col1 0
  11. col7
  12. col2 1
  13. col3 2
  14. col8 2
  15. col4 3
  16. col5
  17. col6 1
  18. Name: row1, dtype: object
  19. >>>
  20. >>> row2=df.loc['row2',:]
  21. >>>
  22. >>> row2
  23. col1 4
  24. col7
  25. col2 5
  26. col3 6
  27. col8 2
  28. col4 7
  29. col5
  30. col6 1
  31. Name: row2, dtype: object
  32. >>>
  33. >>> row12=df.loc[['row1','row2']]
  34. >>>
  35. >>> row12
  36. col1 col7 col2 col3 col8 col4 col5 col6
  37. row1 0 1 2 2 3 1
  38. row2 4 5 6 2 7 1
  39. >>>
  40. >>> row123=df.loc['row1':'row3',:]
  41. >>>
  42. >>> row123
  43. col1 col7 col2 col3 col8 col4 col5 col6
  44. row1 0 1 2 2 3 1
  45. row2 4 5 6 2 7 1
  46. row3 8 9 10 2 11 1
  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. >>>
  7. >>> col1=df.loc[:,'col1']
  8. >>>
  9. >>> col1
  10. row1 0
  11. row2 4
  12. row3 8
  13. Name: col1, dtype: int64
  14. >>> col128=df.loc[:,['col1','col2','col8']]
  15. >>>
  16. >>> col128
  17. col1 col2 col8
  18. row1 0 1 2
  19. row2 4 5 2
  20. row3 8 9 2
  21. >>>
  22. >>> col1723=df.loc[:,'col1':'col3']
  23. >>>
  24. >>> col1723
  25. col1 col7 col2 col3
  26. row1 0 1 2
  27. row2 4 5 6
  28. row3 8 9 10

4、使用条件取相应的行列

  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. # 取出第一列,有对应数字为4的所有行
  7. >>> df.loc[df['col1']==4]
  8. col1 col7 col2 col3 col8 col4 col5 col6
  9. row2 4 5 6 2 7 1
  10. >>>
  11. >>> df.loc[df['col8']==2]
  12. col1 col7 col2 col3 col8 col4 col5 col6
  13. row1 0 1 2 2 3 1
  14. row2 4 5 6 2 7 1
  15. row3 8 9 10 2 11 1
  16. >>> df.loc[~(df['col1']!=4)]
  17. col1 col7 col2 col3 col8 col4 col5 col6
  18. row2 4 5 6 2 7 1
  19. >>>
  20. >>> df.loc[~(df['col1']==4)]
  21. col1 col7 col2 col3 col8 col4 col5 col6
  22. row1 0 1 2 2 3 1
  23. row3 8 9 10 2 11 1

5、使用条件替换行列的值

  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 4 5 6 2 7 1
  5. row3 8 9 10 2 11 1
  6. # 把第一列中值为4的行选取出来,再选择第'col1'列的元素赋值成新的44
  7. >>> df.loc[df['col1']==4,'col1']=44
  8. >>>
  9. >>> df
  10. col1 col7 col2 col3 col8 col4 col5 col6
  11. row1 0 1 2 2 3 1
  12. row2 44 5 6 2 7 1
  13. row3 8 9 10 2 11 1
  14. # 把第一列中值为4的行选取出来,再选择第'col4'列的元素赋值成新的44
  15. >>> df.loc[df['col1']==44,'col4']=44
  16. >>>
  17. >>> df
  18. col1 col7 col2 col3 col8 col4 col5 col6
  19. row1 0 1 2 2 3 1
  20. row2 44 5 6 2 44 1
  21. row3 8 9 10 2 11 1
  22. # 选择'row3'行,其中有值为2的元素赋值成新的44
  23. >>> df.loc['row3',df.loc['row3',:]==2]=44
  24. >>>
  25. >>> df
  26. col1 col7 col2 col3 col8 col4 col5 col6
  27. row1 0 1 2 2 3 1
  28. row2 44 5 6 2 44 1
  29. row3 8 9 10 44 11 1
  30. # 改变某行某列的元素值
  31. >>> df
  32. col1 col2 col3 col4
  33. row1 0 1 2 3
  34. row2 4 5 6 7
  35. row3 8 9 10 11
  36. >>>
  37. >>> df.at['row1','col1']=100
  38. >>>
  39. >>> df
  40. col1 col2 col3 col4
  41. row1 100 1 2 3
  42. row2 4 5 6 7
  43. row3 8 9 10 11

6、使用条件替换整个矩阵的部分元素的值

  1. >>> df
  2. col1 col7 col2 col3 col8 col4 col5 col6
  3. row1 0 1 2 2 3 1
  4. row2 44 5 6 2 44 1
  5. row3 8 9 10 44 11 1
  6. # 将所有值大于40的元素赋值为新的40,注意包含np.nan空值时会报错,需要先去除或者赋值
  7. >>> df[df>40]=40
  8. Traceback (most recent call last):
  9. File "<stdin>", line 1, in <module>
  10. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3114, in __setitem__
  11. self._setitem_frame(key, value)
  12. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 3161, in _setitem_frame
  13. self._check_inplace_setting(value)
  14. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 4503, in _check_inplace_setting
  15. raise TypeError('Cannot do inplace boolean setting on '
  16. TypeError: Cannot do inplace boolean setting on mixed-types with a non np.nan value
  17. >>>
  18. >>>
  19. >>> df>40
  20. col1 col7 col2 col3 col8 col4 col5 col6
  21. row1 False True False False False False True False
  22. row2 True True False False False True True False
  23. row3 False True False False True False True False
  24. >>> df=df.drop(['col7'], axis=1)
  25. >>> df=df.drop(['col5'], axis=1)
  26. >>>
  27. >>> df
  28. col1 col2 col3 col8 col4 col6
  29. row1 0 1 2 2 3 1
  30. row2 44 5 6 2 44 1
  31. row3 8 9 10 44 11 1
  32. >>>
  33. >>> df[df>40]=40
  34. >>>
  35. >>> df
  36. col1 col2 col3 col8 col4 col6
  37. row1 0 1 2 2 3 1
  38. row2 40 5 6 2 40 1
  39. row3 8 9 10 40 11 1

三、DataFrame取元素

1、选择列然后遍历获取元素的值

  1. >>> df
  2. col1 col2 col3 col8 col4 col6
  3. row1 0 1 2 2 3 1
  4. row2 40 5 6 2 40 1
  5. row3 8 9 10 40 11 1
  6. >>> for index in df['col1'].index:
  7. ... idx=df['col1'].get(index)
  8. ... print(idx)
  9. ...
  10. 0
  11. 40
  12. 8

2、使用pandas.DataFrame.at的行索引和列名获取元素的值

  1. >>> df
  2. col1 col2 col3 col8 col4 col6
  3. row1 0 1 2 2 3 1
  4. row2 40 5 6 2 40 1
  5. row3 8 9 10 40 11 1
  6. >>>
  7. >>> df.at[4,'col1']
  8. Traceback (most recent call last):
  9. File "<stdin>", line 1, in <module>
  10. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 2141, in __getitem__
  11. key = self._convert_key(key)
  12. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 2227, in _convert_key
  13. raise ValueError("At based indexing on an non-integer "
  14. ValueError: At based indexing on an non-integer index can only have non-integer indexers
  15. >>>
  16. >>> df.at['row4','col1']
  17. Traceback (most recent call last):
  18. File "<stdin>", line 1, in <module>
  19. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 2142, in __getitem__
  20. return self.obj._get_value(*key, takeable=self._takeable)
  21. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2539, in _get_value
  22. return engine.get_value(series._values, index)
  23. File "pandas/_libs/index.pyx", line 106, in pandas._libs.index.IndexEngine.get_value
  24. File "pandas/_libs/index.pyx", line 114, in pandas._libs.index.IndexEngine.get_value
  25. File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  26. File "pandas/_libs/hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
  27. File "pandas/_libs/hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
  28. KeyError: 'row4'
  29. >>>
  30. >>> df.at['row2','col1']
  31. 40
  32. >>> df.iloc[1].at['col1']
  33. 40

3、使用pandas.DataFrame.iat的行索引和列索引获取元素的值

  1. >>> df
  2. col1 col2 col3 col8 col4 col6
  3. row1 0 1 2 2 3 1
  4. row2 40 5 6 2 40 1
  5. row3 8 9 10 40 11 1
  6. >>>
  7. >>> df.iat[1,2]
  8. 6
  9. >>>
  10. >>> df.iloc[3].iat[4]
  11. Traceback (most recent call last):
  12. File "<stdin>", line 1, in <module>
  13. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1478, in __getitem__
  14. return self._getitem_axis(maybe_callable, axis=axis)
  15. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 2102, in _getitem_axis
  16. self._validate_integer(key, axis)
  17. File "/root/miniconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 2009, in _validate_integer
  18. raise IndexError("single positional indexer is out-of-bounds")
  19. IndexError: single positional indexer is out-of-bounds
  20. >>>
  21. >>> df.iloc[2].iat[4]
  22. 11

4、使用pandas.DataFrame.loc获取元素的值或者行

  1. >>> df
  2. col1 col2 col3 col8 col4 col6
  3. row1 0 1 2 2 3 1
  4. row2 40 5 6 2 40 1
  5. row3 8 9 10 40 11 1
  6. >>>
  7. >>>
  8. >>> df.loc['row1','col1']
  9. 0
  10. >>>
  11. >>> df.loc['row1']
  12. col1 0
  13. col2 1
  14. col3 2
  15. col8 2
  16. col4 3
  17. col6 1
  18. Name: row1, dtype: int64
  19. >>>
  20. >>> df.loc[['row1','row3']]
  21. col1 col2 col3 col8 col4 col6
  22. row1 0 1 2 2 3 1
  23. row3 8 9 10 40 11 1

5、使用pandas.DataFrame.iloc获取元素的值或者行

  1. >>> df
  2. col1 col2 col3 col8 col4 col6
  3. row1 0 1 2 2 3 1
  4. row2 40 5 6 2 40 1
  5. row3 8 9 10 40 11 1
  6. >>>
  7. >>> df.iloc[0,2]
  8. 2
  9. >>>
  10. >>> df.iloc[2]
  11. col1 8
  12. col2 9
  13. col3 10
  14. col8 40
  15. col4 11
  16. col6 1
  17. Name: row3, dtype: int64

引用

【1】https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/153123
推荐阅读
相关标签
  

闽ICP备14008679号