user1700890
user1700890

Reputation: 7732

Pandas - create dataframe manually and insert values

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.

Upvotes: 14

Views: 46227

Answers (2)

hellpanderr
hellpanderr

Reputation: 5896

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

Upvotes: 27

MhP
MhP

Reputation: 56

Providing a sample to increase your data frame dynamically... sizeOfDataFrame variable just limits for loop which adds data to the dataframe and is dynamic...

import pandas as pd
import numpy as np
yourDataFrame = pd.DataFrame()
sizeOfDataFrame = np.random.randint(100, size=1)
for currentLine in range(sizeOfDataFrame):
    yourDataFrame = yourDataFrame.append(pd.DataFrame({"A":np.random.randint(100, size=1),"B":np.random.randint(100, size=1),"C":np.random.randint(100, size=1)},index=[0]))
yourDataFrame.reset_index(inplace = True)    
yourDataFrame.drop('index',axis=1,inplace=True)

Upvotes: 4

Related Questions