Set value for particular cell in pandas DataFrame using index

后端 未结 20 1574
野趣味
野趣味 2020-11-22 05:45

I\'ve created a Pandas DataFrame

df = DataFrame(index=[\'A\',\'B\',\'C\'], columns=[\'x\',\'y\'])

and got this

    x    y
A  NaN         


        
相关标签:
20条回答
  • 2020-11-22 06:49

    I too was searching for this topic and I put together a way to iterate through a DataFrame and update it with lookup values from a second DataFrame. Here is my code.

    src_df = pd.read_sql_query(src_sql,src_connection)
    for index1, row1 in src_df.iterrows():
        for index, row in vertical_df.iterrows():
            src_df.set_value(index=index1,col=u'etl_load_key',value=etl_load_key)
            if (row1[u'src_id'] == row['SRC_ID']) is True:
                src_df.set_value(index=index1,col=u'vertical',value=row['VERTICAL'])
    
    0 讨论(0)
  • 2020-11-22 06:50

    To set values, use:

    df.at[0, 'clm1'] = 0
    
    • The fastest recommended method for setting variables.
    • set_value, ix have been deprecated.
    • No warning, unlike iloc and loc
    0 讨论(0)
提交回复
热议问题