2017-03-15 119 views
0

我想设置一个列的值的基础上另一列值的熊猫数据框一定的值,选择大熊猫数据帧的头几行与列

df2.loc[df2['col1',len] == val, 'col2'] = df1['col2'] 

上面的代码工作正常,然而,现在的问题是,我想设置值仅适用于第几行,类似下面:

len1 = len(df1.index) 
df2.loc[df2['col1',len1] == val, 'col2'] = df1['col2'] 

但我得到以下错误:

Traceback (most recent call last): 
    File "...\lib\site-packages\pandas\indexes\base.py", line 1945, in get_loc 
    return self._engine.get_loc(key) 
    File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4154) 
    File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:4018) 
    File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12368) 

任何帮助将不胜感激。

回答

1

它改成这样:

df2.iloc[:len(df1.index),].ix[df2.col1 == val, 'col2'] = df1['col2'] 

这是工作不知道什么地方错了。

name gender age occupation years_of_school married 
0 Bob  M 37 Dentist    20  N 
1 Sally  F 21 Student    16  N 
2 Jim  M 55 Carpenter    12  Y 
3 Dan  M 27 Teacher    18  Y 
4 Rose  F 14 Student    9  N 
5 Emily  F 65 Retired    22  Y 



    age_range 
0  mid 
1  young 
2  old 
3  young 
4  young 

这里是它的样本查询:

df.iloc[:len(df1.index),].ix[df.age > 25, 'occupation'] = df1['age_range'] 

下面是它返回:

name gender age occupation years_of_school married 
0 Bob  M 37  mid    20  N 
1 Sally  F 21 Student    16  N 
2 Jim  M 55  old    12  Y 
3 Dan  M 27  young    18  Y 
4 Rose  F 14 Student    9  N 
5 Emily  F 65 Retired    22  Y 

我没有收到任何复制片错误。这可能是因为您之前创建或按摩了您的DataFrame,但我只是这样做了,没有错误,没有任何问题。除非我误解了你原来的问题,那么我不明白为什么会倒票,甚至没有解释为什么倒退投票,以便我可以修复它。

+0

但它不会更改df2内col2的值。 – Annie

+0

改变它,现在应该工作 –

+0

http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy – Annie