Python Forum
Limitation in inserting DF
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Limitation in inserting DF
#1
Hello,

There is a limitation in inserting to a data frame.

For example, I have data frame as:

ndf = pd.DataFrame(columns=[‘A’, ‘B’])
ndf.iloc[5,0] = ‘a’
I can’t insert more than 5 values or 5 indexes ?

Please advise !
Reply
#2
It's always good to read built-in help which usually contains useful information:

>>> import pandas as pd
>>> help(pd.DataFrame.iloc)
Help on property:

    Purely integer-location based indexing for selection by position.
    
    ``.iloc[]`` is primarily integer position based (from ``0`` to
    ``length-1`` of the axis), but may also be used with a boolean
    array.
    
    Allowed inputs are:
    
    - An integer, e.g. ``5``.
    - A list or array of integers, e.g. ``[4, 3, 0]``.
    - A slice object with ints, e.g. ``1:7``.
    - A boolean array.
    - A ``callable`` function with one argument (the calling Series, DataFrame
      or Panel) and that returns valid output for indexing (one of the above).
      This is useful in method chains, when you don't have a reference to the
      calling object, but would like to base your selection on some value.
    
    ``.iloc`` will raise ``IndexError`` if a requested indexer is
    out-of-bounds, except *slice* indexers which allow out-of-bounds
    indexing (this conforms with python/numpy *slice* semantics).

/..../ 
You request index which is out of bounds and documentation states that in this case .iloc will raise IndexError.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Sep-10-2019, 07:46 AM)perfringo Wrote: It's always good to read built-in help which usually contains useful information:
 >>> import pandas as pd >>> help(pd.DataFrame.iloc) Help on property: Purely integer-location based indexing for selection by position. ``.iloc[]`` is primarily integer position based (from ``0`` to ``length-1`` of the axis), but may also be used with a boolean array. Allowed inputs are: - An integer, e.g. ``5``. - A list or array of integers, e.g. ``[4, 3, 0]``. - A slice object with ints, e.g. ``1:7``. - A boolean array. - A ``callable`` function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above). This is useful in method chains, when you don't have a reference to the calling object, but would like to base your selection on some value. ``.iloc`` will raise ``IndexError`` if a requested indexer is out-of-bounds, except *slice* indexers which allow out-of-bounds indexing (this conforms with python/numpy *slice* semantics). /..../ 
You request index which is out of bounds and documentation states that in this case .iloc will raise IndexError.

Yes I understand is out of bound and it will state the error but why is out of bound.
I could insert something like the 4, or 3 position etc. what do you advise I use to insert into a data frame by position in row/column ?
Reply
#4
I could not replicate it:

>>> import pandas as pd
>>> df = pd.DataFrame(columns=['a', 'b'])
>>> df.iloc[0, 0] = 'a'
/.../
IndexError: single positional indexer is out-of-bounds
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020