Pandas - create dataframe manually and insert values

user1700890 picture user1700890 · Sep 28, 2015 · Viewed 26.2k times · Source

Here is my code:

import pandas as pd
df = pd.DataFrame(columns = ["A", "B"])
df.iloc[0]['A'] = 5

Here is output:

Traceback (most recent call last):
  File "K:/Dop/Pentas/Simpletest/Temp.py", line 38, in <module>
    df.iloc[0]['A'] = 5
  File "C:\Python34\lib\site-packages\pandas\core\indexing.py", line 1189, in __getitem__
    return self._getitem_axis(key, axis=0)
  File "C:\Python34\lib\site-packages\pandas\core\indexing.py", line 1480, in _getitem_axis
    return self._get_loc(key, axis=axis)
  File "C:\Python34\lib\site-packages\pandas\core\indexing.py", line 89, in _get_loc
    return self.obj._ixs(key, axis=axis)
  File "C:\Python34\lib\site-packages\pandas\core\frame.py", line 1719, in _ixs
    label = self.index[i]
  File "C:\Python34\lib\site-packages\pandas\core\index.py", line 1076, in __getitem__
    return getitem(key)
IndexError: index 0 is out of bounds for axis 0 with size 0

Any suggestions on how to fix it? I do not know overall size of my dataframe before hand, but I can guess.

Answer

hellpanderr picture hellpanderr · Sep 28, 2015

You can either initialize dataframe with data using

df = pd.DataFrame(columns=["A", "B"], data=[[5,np.nan]]),

or use set_value method (which is much faster than iloc by the way): df.set_value(0,'A',5)

UPDATE 2018-04-12

Since pandas version 0.21.0 df.set_value is deprecated. You should use .at[]or .iat[] accessors instead:

df.at[0, 'A'] = 5