KeyErr 处的熊猫排成一行

问题描述

我将 csv 加载到 Pandas

TabR = pd.read_csv('/home/kai/Dokumente/Python/Daten/mst10.csv',skiprows=[0,1],delimiter=',')

这是原始信息:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 34254 entries,0 to 34253
Data columns (total 5 columns):
 #   Column              Non-Null Count  Dtype  
---  ------              --------------  -----  
 0   Nr.                 34254 non-null  object 
 1   Tag                 34254 non-null  object 
 2   Datum               34254 non-null  object 
 3   Uhrzeit             34254 non-null  object 
 4   Summe(mst10r) [mm]  34254 non-null  float64
dtypes: float64(1),object(4)
memory usage: 1.6+ MB
None

我用这条线改变了两行

TabR = TabR.rename(index=str,columns={"Summe(mst10r) [mm]" : "Mst10"})

TabR['RSumme'] = np.nan

这是新信息

    <class 'pandas.core.frame.DataFrame'>
Index: 34254 entries,0 to 34253
Data columns (total 6 columns):
 #   Column   Non-Null Count  Dtype  
---  ------   --------------  -----  
 0   Nr.      34254 non-null  object 
 1   Tag      34254 non-null  object 
 2   Datum    34254 non-null  object 
 3   Uhrzeit  34254 non-null  object 
 4   Mst10    34254 non-null  float64
 5   RSumme   0 non-null      float64
dtypes: float64(2),object(4)
memory usage: 1.8+ MB
None

我想在 Mst10 列中获取某些值。我向我们尝试了 .at 方法,但它不起作用。我总是会得到一个 KeyErr。

print(TabR.at[4,'Mst10'])

这是错误消息。

Traceback (most recent call last):

  File "/home/kai/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py",line 3080,in get_loc
    return self._engine.get_loc(casted_key)

  File "pandas/_libs/index.pyx",line 70,in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/index.pyx",line 101,in pandas._libs.index.IndexEngine.get_loc

  File "pandas/_libs/hashtable_class_helper.pxi",line 4554,in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas/_libs/hashtable_class_helper.pxi",line 4562,in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 4


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "/home/kai/Dokumente/Python/Auswertung_Matplotlib.py",line 75,in <module>
    print(TabR.at[4,'Mst10'])

  File "/home/kai/.local/lib/python3.8/site-packages/pandas/core/indexing.py",line 2156,in __getitem__
    return super().__getitem__(key)

  File "/home/kai/.local/lib/python3.8/site-packages/pandas/core/indexing.py",line 2103,in __getitem__
    return self.obj._get_value(*key,takeable=self._takeable)

  File "/home/kai/.local/lib/python3.8/site-packages/pandas/core/frame.py",line 3144,in _get_value
    index = self.index.get_loc(index)

  File "/home/kai/.local/lib/python3.8/site-packages/pandas/core/indexes/base.py",line 3082,in get_loc
    raise KeyError(key) from err

KeyError: 4

为什么会出现这个错误?列和行存在.... ??

最好的问候 凯

阿姨编辑: 我认为我的索引是字符串。 如果我尝试

  print(TabR.at['4','Mst10'])

即使在循环中也能工作。

for i in range(len(TabR)):
    if i == 0:
        print(i)
        TabR.at[str(i),'RSumme']=TabR.at[str(i),'Mst10']
    else:
        TabR.at[str(i),'Mst10']+TabR.at[str(i-1),'Mst10']

但是为什么我的索引 afre 字符串不是 int ?

最好的问候 凯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)