数据帧,以便一列为键,另一列为值

时间:2018-12-27 07:18:30

标签: python pandas dictionary dataframe

我有数据框

    ID   A   B   C
0   p    1   3   2
1   q    4   3   2
2   r    4   0   9  

我想创建一个字典,其中ID是键,B是值,因此它将是: d [“ q”] = 3,d [“ r”] = 0 最好的方法是什么?

它与假定的重复项有所不同,因为我希望每个键一个值而不是一个列表

2 个答案:

答案 0 :(得分:2)

类似这样的东西:

    private void OnClick(int position)
    {
        position++;
        if (position == 1)
        {
            Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
        }
        else if (position == 2)
        {
            Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
        }
        else if (position == 3)
        {
            Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();
        }
        else if (position == 4)
        {
            Toast.MakeText(this, $"you click title" + position, ToastLength.Short).Show();

        }

        DrawerLayout drawer = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        drawer.CloseDrawer(GravityCompat.Start);

    }

答案 1 :(得分:1)

df = pd.DataFrame([['p',1,3,2],['q',4,3,2],['r',4,0,9]], columns=['ID','A','B','C'])
df=df[['ID','A','B','C']]

df

  ID  A  B  C
0  p  1  3  2
1  q  4  3  2
2  r  4  0  9

your_dict = dict(zip(df.ID, df.B))

your_dict
 {'p': 3, 'q': 3, 'r': 0}