import numpy as np
from sklearn.neighbors import NearestNeighbors
import pandas as pd

# Create a random DataFrame
df = pd.DataFrame(np.random.rand(10, 2), columns=['x', 'y'])

# Create a NearestNeighbors object
nn = NearestNeighbors(n_neighbors=2)

# Fit the NearestNeighbors object to the data
nn.fit(df[['x', 'y']])

# Calculate the nearest neighbors for each row
distances, indices = nn.kneighbors(df[['x', 'y']])
nearest_indices = indices[:, 1]

# Add a new column to the DataFrame with the nearest indices
df['nearest_index'] = nearest_indices

df