Creating NFT Collection from CSV Data with Pandas - Need Help

I’ve been working on creating NFTs from CSV metadata for weeks now and I’m really stuck. Been following tutorials but something isn’t working right in my code.

I’m trying to generate unique NFT combinations and check if they already exist in my CSV file. Here’s what I have so far:

def createRandomNFT(tokenID):
    NAME = "CryptoArt" + str(tokenID)
    ID = tokenID
    BACKGROUND = getRandomBackground()
    PROPS = getRandomProps()
    EYES = getRandomEyes()
    HAIR = getRandomHair()
    SKIN = getRandomSkin()
    OUTFIT = getRandomOutfit()
    HANDS = getRandomHands()
    MOUTH = getRandomMouth()
    newRow = [NAME,ID,BACKGROUND,PROPS,EYES,HAIR,SKIN,OUTFIT,HANDS,MOUTH]

sampleRow = ["CryptoArt1234","1234","galaxy","glasses","blue","spiky","tan","hoodie","gloves","smile"]

def doesCombinationExist(rowToCheck):
    df = pd.read_csv('nft_data.csv')
    
    matches = df[(df['Background'] == rowToCheck[2])] & (df['Props'] == rowToCheck[3]) & (df['Eyes'] == rowToCheck[4]) & (df['Hair'] == rowToCheck[5]) & (df['Skin'] == rowToCheck[6]) & (df['Outfit'] == rowToCheck[7]) & (df['Hands'] == rowToCheck[8]) & (df['Mouth'] == rowToCheck[9]).index.tolist()
    print(matches)
    
    if matches == []:
        return False
    else:
        return True

doesCombinationExist(sampleRow)

I keep getting errors when I run this. Can someone help me figure out what’s wrong? I’m pretty new to Python so any help would be awesome!

Hit the same issue building my first NFT generator last year. Your pandas filtering is missing parentheses around each condition - that’s what’s breaking it. When you chain multiple & operators, pandas needs every comparison wrapped in parentheses.

Try this:

matches = df[(df['Background'] == rowToCheck[2]) & 
             (df['Props'] == rowToCheck[3]) & 
             (df['Eyes'] == rowToCheck[4]) & 
             (df['Hair'] == rowToCheck[5]) & 
             (df['Skin'] == rowToCheck[6]) & 
             (df['Outfit'] == rowToCheck[7]) & 
             (df['Hands'] == rowToCheck[8]) & 
             (df['Mouth'] == rowToCheck[9])]

Ditch the .index.tolist() at the end - just check if the dataframe is empty with len(matches) == 0. Also make sure your function returns the newRow variable, otherwise you won’t get the generated NFT data back.

Oh wow, this looks like a really cool project! I’ve been thinking about getting into NFT generation myself.

What errors are you getting exactly? That would help narrow down the issue. Also noticed you’re calling doesCombinationExist(sampleRow) but not using the return value - planning to use that boolean somewhere else?

One thing that caught my eye: you’re reading the CSV file every time you check for duplicates. If you’re generating lots of NFTs, that’ll get pretty slow. Consider loading the dataframe once at the beginning and passing it to your function instead.

How big is your CSV file getting? What’s your target collection size? Seems like a fun project!

hey! just a quick tip - the parentheses in your pandas condition are jumbled. try wrapping each condition in its own set of parentheses like this: (df['Background'] == rowToCheck[2]) & (df['Props'] == rowToCheck[3]) and so on. also, your createRandomNFT function should return newRow.