pandas: Unable to write values to single row dataframe










1














I have a single row dataframe(df) on which I want to insert value for every column using only the index numbers.
The dataframe df is in following form.



 a b c
1 0 0 0
2 0 0 0
3 0 0 0

df.iloc[[0],[1]] = predictions[:1]


This gives me the following warning and does not write anything to the row:



SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead


However when I try using



pred_row.iloc[0,1] = predictions[:1]


It gives me error



ValueError: Incompatible indexer with Series


Is there a way to write value to single row dataframe.
Predictions is any random value that I am trying to set in a particular cell of df










share|improve this question























  • What is predictions ? Can you add some sample?
    – jezrael
    Nov 12 at 6:06










  • @jezrael prediction is any random value that I am predicting and adding it to a column in df
    – apoorv parmar
    Nov 12 at 6:07










  • so what is print (type(predictions[:1])) ?
    – jezrael
    Nov 12 at 6:08










  • @jezrael <class 'pandas.core.series.Series'>
    – apoorv parmar
    Nov 12 at 6:12






  • 1




    Can you add yout complete code, what do you try? Because is not possible set one element of DataFrame by Series - array. Maybe need df.iloc[0, :] = predictions[:1].values if want set first row of df by predictions[:1]
    – jezrael
    Nov 12 at 6:15















1














I have a single row dataframe(df) on which I want to insert value for every column using only the index numbers.
The dataframe df is in following form.



 a b c
1 0 0 0
2 0 0 0
3 0 0 0

df.iloc[[0],[1]] = predictions[:1]


This gives me the following warning and does not write anything to the row:



SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead


However when I try using



pred_row.iloc[0,1] = predictions[:1]


It gives me error



ValueError: Incompatible indexer with Series


Is there a way to write value to single row dataframe.
Predictions is any random value that I am trying to set in a particular cell of df










share|improve this question























  • What is predictions ? Can you add some sample?
    – jezrael
    Nov 12 at 6:06










  • @jezrael prediction is any random value that I am predicting and adding it to a column in df
    – apoorv parmar
    Nov 12 at 6:07










  • so what is print (type(predictions[:1])) ?
    – jezrael
    Nov 12 at 6:08










  • @jezrael <class 'pandas.core.series.Series'>
    – apoorv parmar
    Nov 12 at 6:12






  • 1




    Can you add yout complete code, what do you try? Because is not possible set one element of DataFrame by Series - array. Maybe need df.iloc[0, :] = predictions[:1].values if want set first row of df by predictions[:1]
    – jezrael
    Nov 12 at 6:15













1












1








1







I have a single row dataframe(df) on which I want to insert value for every column using only the index numbers.
The dataframe df is in following form.



 a b c
1 0 0 0
2 0 0 0
3 0 0 0

df.iloc[[0],[1]] = predictions[:1]


This gives me the following warning and does not write anything to the row:



SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead


However when I try using



pred_row.iloc[0,1] = predictions[:1]


It gives me error



ValueError: Incompatible indexer with Series


Is there a way to write value to single row dataframe.
Predictions is any random value that I am trying to set in a particular cell of df










share|improve this question















I have a single row dataframe(df) on which I want to insert value for every column using only the index numbers.
The dataframe df is in following form.



 a b c
1 0 0 0
2 0 0 0
3 0 0 0

df.iloc[[0],[1]] = predictions[:1]


This gives me the following warning and does not write anything to the row:



SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead


However when I try using



pred_row.iloc[0,1] = predictions[:1]


It gives me error



ValueError: Incompatible indexer with Series


Is there a way to write value to single row dataframe.
Predictions is any random value that I am trying to set in a particular cell of df







python pandas dataframe






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 6:11

























asked Nov 12 at 6:02









apoorv parmar

317




317











  • What is predictions ? Can you add some sample?
    – jezrael
    Nov 12 at 6:06










  • @jezrael prediction is any random value that I am predicting and adding it to a column in df
    – apoorv parmar
    Nov 12 at 6:07










  • so what is print (type(predictions[:1])) ?
    – jezrael
    Nov 12 at 6:08










  • @jezrael <class 'pandas.core.series.Series'>
    – apoorv parmar
    Nov 12 at 6:12






  • 1




    Can you add yout complete code, what do you try? Because is not possible set one element of DataFrame by Series - array. Maybe need df.iloc[0, :] = predictions[:1].values if want set first row of df by predictions[:1]
    – jezrael
    Nov 12 at 6:15
















  • What is predictions ? Can you add some sample?
    – jezrael
    Nov 12 at 6:06










  • @jezrael prediction is any random value that I am predicting and adding it to a column in df
    – apoorv parmar
    Nov 12 at 6:07










  • so what is print (type(predictions[:1])) ?
    – jezrael
    Nov 12 at 6:08










  • @jezrael <class 'pandas.core.series.Series'>
    – apoorv parmar
    Nov 12 at 6:12






  • 1




    Can you add yout complete code, what do you try? Because is not possible set one element of DataFrame by Series - array. Maybe need df.iloc[0, :] = predictions[:1].values if want set first row of df by predictions[:1]
    – jezrael
    Nov 12 at 6:15















What is predictions ? Can you add some sample?
– jezrael
Nov 12 at 6:06




What is predictions ? Can you add some sample?
– jezrael
Nov 12 at 6:06












@jezrael prediction is any random value that I am predicting and adding it to a column in df
– apoorv parmar
Nov 12 at 6:07




@jezrael prediction is any random value that I am predicting and adding it to a column in df
– apoorv parmar
Nov 12 at 6:07












so what is print (type(predictions[:1])) ?
– jezrael
Nov 12 at 6:08




so what is print (type(predictions[:1])) ?
– jezrael
Nov 12 at 6:08












@jezrael <class 'pandas.core.series.Series'>
– apoorv parmar
Nov 12 at 6:12




@jezrael <class 'pandas.core.series.Series'>
– apoorv parmar
Nov 12 at 6:12




1




1




Can you add yout complete code, what do you try? Because is not possible set one element of DataFrame by Series - array. Maybe need df.iloc[0, :] = predictions[:1].values if want set first row of df by predictions[:1]
– jezrael
Nov 12 at 6:15




Can you add yout complete code, what do you try? Because is not possible set one element of DataFrame by Series - array. Maybe need df.iloc[0, :] = predictions[:1].values if want set first row of df by predictions[:1]
– jezrael
Nov 12 at 6:15












1 Answer
1






active

oldest

votes


















0














For set one element of Series to DataFrame change selecting to predictions[0]:



print (df)
a b c
1 0 0 0
2 0 0 0
3 0 0 0

predictions = pd.Series([1,2,3])
print (predictions)
0 1
1 2
2 3
dtype: int64

df.iloc[0, 1] = predictions[0]
#more general for set one element of Series by position
#df.iloc[0, 1] = predictions.iat[0]
print (df)
a b c
1 0 1 0
2 0 0 0
3 0 0 0


Details:



#scalar 
print (predictions[0])
1

#one element Series
print (predictions[:1])
0 1
dtype: int64



Also working convert one element Series to one element array, but set by scalar is simplier:



df.iloc[0, 1] = predictions[:1].values
print (df)
a b c
1 0 1 0
2 0 0 0
3 0 0 0

print (predictions[:1].values)
[1]





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256616%2fpandas-unable-to-write-values-to-single-row-dataframe%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    For set one element of Series to DataFrame change selecting to predictions[0]:



    print (df)
    a b c
    1 0 0 0
    2 0 0 0
    3 0 0 0

    predictions = pd.Series([1,2,3])
    print (predictions)
    0 1
    1 2
    2 3
    dtype: int64

    df.iloc[0, 1] = predictions[0]
    #more general for set one element of Series by position
    #df.iloc[0, 1] = predictions.iat[0]
    print (df)
    a b c
    1 0 1 0
    2 0 0 0
    3 0 0 0


    Details:



    #scalar 
    print (predictions[0])
    1

    #one element Series
    print (predictions[:1])
    0 1
    dtype: int64



    Also working convert one element Series to one element array, but set by scalar is simplier:



    df.iloc[0, 1] = predictions[:1].values
    print (df)
    a b c
    1 0 1 0
    2 0 0 0
    3 0 0 0

    print (predictions[:1].values)
    [1]





    share|improve this answer



























      0














      For set one element of Series to DataFrame change selecting to predictions[0]:



      print (df)
      a b c
      1 0 0 0
      2 0 0 0
      3 0 0 0

      predictions = pd.Series([1,2,3])
      print (predictions)
      0 1
      1 2
      2 3
      dtype: int64

      df.iloc[0, 1] = predictions[0]
      #more general for set one element of Series by position
      #df.iloc[0, 1] = predictions.iat[0]
      print (df)
      a b c
      1 0 1 0
      2 0 0 0
      3 0 0 0


      Details:



      #scalar 
      print (predictions[0])
      1

      #one element Series
      print (predictions[:1])
      0 1
      dtype: int64



      Also working convert one element Series to one element array, but set by scalar is simplier:



      df.iloc[0, 1] = predictions[:1].values
      print (df)
      a b c
      1 0 1 0
      2 0 0 0
      3 0 0 0

      print (predictions[:1].values)
      [1]





      share|improve this answer

























        0












        0








        0






        For set one element of Series to DataFrame change selecting to predictions[0]:



        print (df)
        a b c
        1 0 0 0
        2 0 0 0
        3 0 0 0

        predictions = pd.Series([1,2,3])
        print (predictions)
        0 1
        1 2
        2 3
        dtype: int64

        df.iloc[0, 1] = predictions[0]
        #more general for set one element of Series by position
        #df.iloc[0, 1] = predictions.iat[0]
        print (df)
        a b c
        1 0 1 0
        2 0 0 0
        3 0 0 0


        Details:



        #scalar 
        print (predictions[0])
        1

        #one element Series
        print (predictions[:1])
        0 1
        dtype: int64



        Also working convert one element Series to one element array, but set by scalar is simplier:



        df.iloc[0, 1] = predictions[:1].values
        print (df)
        a b c
        1 0 1 0
        2 0 0 0
        3 0 0 0

        print (predictions[:1].values)
        [1]





        share|improve this answer














        For set one element of Series to DataFrame change selecting to predictions[0]:



        print (df)
        a b c
        1 0 0 0
        2 0 0 0
        3 0 0 0

        predictions = pd.Series([1,2,3])
        print (predictions)
        0 1
        1 2
        2 3
        dtype: int64

        df.iloc[0, 1] = predictions[0]
        #more general for set one element of Series by position
        #df.iloc[0, 1] = predictions.iat[0]
        print (df)
        a b c
        1 0 1 0
        2 0 0 0
        3 0 0 0


        Details:



        #scalar 
        print (predictions[0])
        1

        #one element Series
        print (predictions[:1])
        0 1
        dtype: int64



        Also working convert one element Series to one element array, but set by scalar is simplier:



        df.iloc[0, 1] = predictions[:1].values
        print (df)
        a b c
        1 0 1 0
        2 0 0 0
        3 0 0 0

        print (predictions[:1].values)
        [1]






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 6:36

























        answered Nov 12 at 6:22









        jezrael

        318k22257336




        318k22257336



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256616%2fpandas-unable-to-write-values-to-single-row-dataframe%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto