Search code examples
pythonpymongobson

Python bson: How to create list of ObjectIds in a New Column


I have a CSV that I'm needing to create a column of random unique MongoDB ids in python.

Here's my csv file:

   import pandas as pd

   df = pd.read_csv('file.csv', sep=';')
   print(df)

        Zone
        Zone_1
        Zone_2

I'm currently using this line of code to generate a unique ObjectId - UPDATE

import bson

x = bson.objectid.ObjectId()
df['objectids'] = x
print(df)

Zone; objectids
Zone_1; 5bce2e42f6738f20cc12518d
Zone_2; 5bce2e42f6738f20cc12518d

How can I get the ObjectId to be unique for each row?


Solution

  • Hate to see you down voted... stack overflow goes nuts with the duplicate question nonsense, refuses to provide useful assistance, then down votes you for having the courage to ask about something you don't know.

    The referenced question clearly has nothing to do with ObjectIds, let alone adding them (or any other object not internal to NumPy or Pandas) into data frames.

    You probably need to use a map

    this assumes the column "objectids" is not a Series in your frame and "Zone" is a Series in your frame

    df['objectids'] = df['Zone'].map(lambda x: bson.objectid.ObjectId())

    Maps are super helpful (though slow) way to poke every record in your series and particularly helpful as an initial method to connect external functions.

    https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html