import numpy as np
import pandas as pd
# Create a random DataFrame
df = pd.DataFrame(np.random.rand(10, 2), columns=['x', 'y'])
# Calculate the Euclidean distances between each row
distances = np.sqrt(((df.values[:, np.newaxis, :] - df.values[np.newaxis, :, :]) ** 2).sum(axis=2))
# Find the nearest index for each row
nearest_indices = distances.argsort()[:, 1]
# Add a new column to the DataFrame with the nearest indices
df['nearest_index'] = nearest_indices
df