迭代并更改以熊猫为单位的行的值(错误“ [index]中[Int64Index([10],dtype ='int64')]都不存在”)

时间:2019-02-20 09:35:48

标签: python python-3.x pandas dataframe jupyter-notebook

result = authContext.AcquireTokenAsync(todoListResourceId, clientId, new Uri("http://TodoListClient-Headless"), new PlatformParameters(PromptBehavior.Auto)).Result;

基本上,我想迭代每一行并想选择c1列的值(示例-在第一次迭代中,我将获得值10) 比起取c1的值,我想改变 raw [row [''c1]]改为XX(示例-我们得到10的值,现在我想将raw ['10']的值更改为XX

我尝试过:

import pandas as pd

inp = [{'c1':10, '10':100, '11':100, '12':100},   
       {'c1':11,'10':110, '11':100, '12':100},  
       {'c1':12,'10':120, '11':100, '12':100}] .   

df = pd.DataFrame(inp)

     10      11      12     c1
0   100     100     100     10   
1   110     100     100     11   
2   120     100     100     12   

Expected Output ::

     10      11      12     c1
0   XX      100     100     10   
1   110     XX      100     11   
2   120     100     XX      12    

2 个答案:

答案 0 :(得分:0)

您可以这样做:

res = df.copy()

for i in df.iterrows():
    res.loc[i[0], str(i[1]['c1'])] = 'xx'

res

    10      11      12  c1
0   xx     100     100  10
1   110     xx     100  11
2   120     100     xx  12

答案 1 :(得分:0)

不建议在大熊猫中圈起来,因为它很慢,但是如果需要的话:

for row in df.itertuples(index=True, name='Pandas'):  
    variable=getattr(row, "c1")  
    df.loc[row.Index, str(variable)]= 'XX'  

print (df)
    10   11   12  c1
0   XX  100  100  10
1  110   XX  100  11
2  120  100   XX  12

您也可以按字典循环:

for k, v in df['c1'].to_dict().items():
    df.loc[k, str(v)] = 'XX'