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.
append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.
loc: loc is an integer which is the location of column where we want to insert new column. This will shift the existing column at that position to the right. column: column is a string which is name of column to be inserted. value: value is simply the value to be inserted.
To create a dataframe, we need to import pandas. Dataframe can be created using dataframe() function. The dataframe() takes one or two parameters. The first one is the data which is to be filled in the dataframe table.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With