Home > front end >  Pandas : Empty dataframe after using concat()
Pandas : Empty dataframe after using concat()

Time:04-27

I created an empty datafame,drugs:

drugs = pd.DataFrame({'name':[],
                    'value':[]})

after that, I wanted to add data from another data frame to it using a for loop:

for i in range(227,498):
    value = drug_users[drug_users[drug_users.columns[i]] == 1][drug_users.columns[i]].sum() / 10138
    name = drug_users.columns[i]
    d2 = pd.DataFrame({'name':[name],
                    'value':[value]})
    print(d2)
    pd.concat([drugs, d2], ignore_index = True, axis = 0)

but when I take a sample from drugs, I get the error:

ValueError: a must be greater than 0 unless no samples are taken

CodePudding user response:

The concat method return a new dataframe instead of changing the current dataframe. You need to assign the return value, e.g.:

drugs = pd.concat([drugs, d2], ignore_index = True, axis = 0)

CodePudding user response:

You need to assign the return value from the concat function, I'm afraid it is not an inplace operation.

  • Related