KeyError: 750 in Pandas DataFrame: How to Handle Missing Keys
This error occurs because the key '750' is not present in the RangeIndex of the DataFrame.
To resolve this issue, you can check if the key exists in the RangeIndex before accessing it. You can use the in operator to check if the key is present in the RangeIndex.
Here's an example of how you can modify your code to handle this error:
def __getitem__(self, idx):
edge_index = torch.tensor(self.edges[['source', 'target']].values.T, dtype=torch.long)
if idx in self.features.index:
x = self.features[idx]
y = self.labels[idx]
else:
# Handle the case when the key is not present in the RangeIndex
# You can raise an exception or return a default value
x = None
y = None
if self.transform is not None:
# Apply any transformations if needed
x = self.transform(x)
return x, y
By checking if the key exists in the RangeIndex before accessing it, you can handle the case when the key is not present and avoid the KeyError.
原文地址: https://www.cveoy.top/t/topic/pbMG 著作权归作者所有。请勿转载和采集!