这是我的代码:
import pandas as pd
df = pd.DataFrame(columns = ["A", "B"])
df.iloc[0]['A'] = 5
这是输出:
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
有关如何修复它的任何建议?我不知道我的数据帧的总体大小,但我可以猜测。
答案 0 :(得分:11)
您可以使用
使用数据初始化数据框 df = pd.DataFrame(columns=["A", "B"], data=[[5,np.nan]])
,
或使用set_value
方法(顺便说一下,它比iloc
快得多):
df.set_value(0,'A',5)
更新2018-04-12 ⬇
由于不推荐使用pandas版本0.21.0 df.set_value
。您应该使用.at[]
或.iat[]
访问者:
df.at[0, 'A'] = 5
答案 1 :(得分:4)
提供样本以动态增加数据框... sizeOfDataFrame变量仅限于循环,它将数据添加到数据框并且是动态的......
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)