得到KeyError:' [1.4 nan nan 1.83]不在索引'

时间:2017-12-05 06:48:31

标签: python python-3.x pandas

我想在下面添加我想要的输出值。

我正在使用:

#O1 Mangoes
aa = df2.AD=np.where(df3.AL.isin(df1.EW),df2.AD,np.nan)

df3['O1'] = df1[aa]

#O2 Curries
zz = df1.WE=np.where(df3.AL.isin(df1.EW),df1.WE,np.nan)
print(zz)
df3['O2'] = df1[zz]

我一直在:

KeyError: '[ 1.4    nan   nan  1.83] not in index'

一切都是正确的,除了我想把它转移到O1和O2列以及放弃NaN

我尝试使用:df = df3[pd.notnull(df3['TN'])]并写入此内容以获得优秀但我的输出如下:

enter image description here

DF1

                                        EW    WE           DA
0  Persebaya Surabaya v PS Mojokerto Putra  1.40  https://www
1                                JP Voltes  3.40  https://www
2                                  pp v pp  3.10  https://www
3                                    Gagra  1.83  https://www

DF2

            AA                  AB                  AC     AD            AE
0  Today 18:00  Persebaya Surabaya  PS Mojokerto Putra   1.95  https://www.
1  Today 18:00    Davao Aguilas FC           JP Voltes   1.95  https://www.
2  Today 18:30     Adelaide United              Sydney   1.66  https://www.
3  Today 20:00                  pp                  pp  16.00  https://www.

Desired(我已经知道了,保存更改为O列)

           DAT                                        G  \
0  Today 18:00  Persebaya Surabaya v PS Mojokerto Putra   
1  Today 18:00             Davao Aguilas FC v JP Voltes   
2  Today 18:30                 Adelaide United v Sydney   
3  Today 20:00                                  pp v pp   

                                        TN   O1            L1  \
0  Persebaya Surabaya v PS Mojokerto Putra  1.4  https://www.   
1                                      NaN  NaN  https://www.   
2                                      NaN  NaN  https://www.   
3                                  pp v pp  3.10  https://www.   

                                       TN2   O2           L2    D   AJ  \
0  Persebaya Surabaya v PS Mojokerto Putra  1.95  https://www  NaN  NaN   
1                                      NaN  NaN  https://www  NaN  NaN   
2                                      NaN  NaN  https://www  NaN  NaN   
3                                  pp v pp  16  https://www  NaN  NaN   

                                        AK  
0  Persebaya Surabaya v PS Mojokerto Putra  
1                                      NaN  
2                                      NaN  
3                                  pp v pp  

完整code

感兴趣的代码:

##O  <-- Get value beside T

    cs = df2[df2.AD.isin(df1.EW)]
    df3 = df3[['AJ','AK']]
    print (cs)

1 个答案:

答案 0 :(得分:0)

您的keyerror错误表示没有包含此名称的列。

如果要将df3列分配到df2df1,则所有数据框和相同索引值的长度也必须相同。

df2.AD = np.where(df3.AL.isin(df1.EW),df2.AD,np.nan)
#if want assign one column to another
df3['O1'] = df2.AD