Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'DataFrame' object has no attribute 'set_value'

Tags:

python

flask

I'm using flask and getting error at set_values. I'm reading the input from html and passing it to the code

@app.route('/home', methods=['POST'])
def first():
    source = request.files['first']
    destination = request.files['second']
    df = pd.read_csv(source)
    df1 = pd.read_csv(destination)
    val1 = int(request.form['val1'])
    val2 = int(request.form['val2'])
    val3 = int(request.form['val3'])
    target = request.form['str']
    df2 = df[df.columns[val2]]
    count = 0
    for j in df[df.columns[val1]]:
        x = df1.loc[df1[df1.columns[val3]] == j].index.values
        for i in x:
            df1.set_value(i, target, df2[count])
        count = count + 1
    df1.to_csv('result.csv', index=False)
like image 732
NAVYA REDDY Avatar asked Feb 19 '20 06:02

NAVYA REDDY


People also ask

What is Set_value in Python?

set_value() function to set value of a particular index. # set value of a cell which has index label "8" and column label "8" df.set_value( 8 , 8 , 1000 ) Output : Notice, for the non-existent row and column in the dataframe, a new row and column has been inserted.

How do you set a value in a data frame?

You can set cell value of pandas dataframe using df.at[row_label, column_label] = 'Cell Value'. It is the fastest method to set the value of the cell of the pandas dataframe. Dataframe at property of the dataframe allows you to access the single value of the row/column pair using the row and column labels.


1 Answers

Check your pandas version.

df.set_value() is deprecated since pandas version 0.21.0

Instead use df.at

import pandas as pd

df = pd.DataFrame({"A":[1, 5, 3, 4, 2], 
                   "B":[3, 2, 4, 3, 4],  
                   "C":[2, 2, 7, 3, 4], 
                   "D":[4, 3, 6, 12, 7]}) 

df.at[2,'B']=100

   A    B  C   D
0  1    3  2   4
1  5    2  2   3
2  3  100  7   6
3  4    3  3  12
4  2    4  4   7
like image 86
Asetti sri harsha Avatar answered Sep 21 '22 21:09

Asetti sri harsha