根据列表填充数据框列

时间:2018-04-16 18:56:45

标签: python pandas dataframe

我有一个数据框

vehicle_make    vehicle_model   vehicle_year
Toyota  Corolla 2016
Hyundai Sonata  2016
Cadillac    DTS 2006
Toyota  Prius   2014
Kia Optima  2015

我想添加一个新列'vehicle_make_category',根据我有的列表填充

luxury=['Bentley',
'Maserati',
'Hummer',
'Porsche',
'Lexus']

non_luxury=['Saab',
'Mazda',
'Dodge',
'Volkswagen',
'Kia',
'Chevrolet',
'Hyundai',
'Ford',
'Nissan',
'Honda',
'Toyota'
]

怎么能做到这一点?我尝试过使用

df['vehicle_make_category']=np.where(df['vehicle_make']=i for i in luxury, 'luxury')

但它不起作用......

5 个答案:

答案 0 :(得分:2)

简单

string hozzaadnivalo;

public override Java.Lang.Object InstantiateItem(ViewGroup container, int position)
        {

            View view = LayoutInflater.From(container.Context).Inflate(Resource.Layout.pager_item, container, false);
            container.AddView(view);

            Button hozzaadas = view.FindViewById<Button>(Resource.Id.hozzaad);

            var autoCompleteOptions = new string[] { "Sajt", "Tej", "Kecske", "Barátnő", "piros", "alma" };
            ArrayAdapter autoCompleteAdapter = new ArrayAdapter(container.Context, Android.Resource.Layout.SimpleDropDownItem1Line, autoCompleteOptions);
            AutoCompleteTextView mautoCompleteTextView = view.FindViewById<AutoCompleteTextView>(Resource.Id.autoCompleteTextView1);
            mautoCompleteTextView.Adapter = autoCompleteAdapter;

            hozzaadas.Click += hozaadasListViewhez;
            hozzaadnivalo = mautoCompleteTextView.Text;
        }
private void hozaadasListViewhez(object sender, EventArgs e)
        {
            adapter.Add(mautoCompleteTextView.Text);
            adapter.NotifyDataSetChanged();
        }

答案 1 :(得分:1)

使用isin并向np.where添加一个条件,填补未评估为真的条件的空白

df['vehicle_make_category'] = np.where(df.vehicle_make.isin(luxury),'luxury','non-luxury')

  vehicle_make vehicle_model  vehicle_year vehicle_make_category
0       Toyota       Corolla          2016            non-luxury
1      Hyundai        Sonata          2016            non-luxury
2     Cadillac           DTS          2006            non-luxury
3       Toyota         Prius          2014            non-luxury
4          Kia        Optima          2015            non-luxury

使用np.select我们可以创建条件列表并根据条件为真值

分配值
conditions = [df.vehicle_make.isin(luxury),df.vehicle_make.isin(non_luxury)]

df['vehicle_make_category'] = np.select(conditions,['luxury','non-luxury'],default='no-category')

  vehicle_make vehicle_model  vehicle_year vehicle_make_category
0       Toyota       Corolla          2016            non-luxury
1      Hyundai        Sonata          2016            non-luxury
2     Cadillac           DTS          2006           no-category
3       Toyota         Prius          2014            non-luxury
4          Kia        Optima          2015            non-luxury

答案 2 :(得分:0)

您可以通过列表理解创建列:

df['vehicle_make_category'] = [
    'luxury' if row.vehicle_make in luxury
    else 'non_luxury'
    for _, row in df.iterrows()
]

答案 3 :(得分:0)

你可以我们df.join

您必须制作一个新的数据框架来识别奢侈品/非奢侈品。

veh = ['toyota','hyundai','cadillac']
yr = [2016,2016,2016]
lux = ['non','non','lux']

#recreating your lux/non layout
n_lux = [veh[0],veh[1]]
lux = [veh[2]]

#then making a new column
b = ['non' if v in n_lux else 'lux' for v in veh]


A = pd.DataFrame(np.array([veh,yr]).T)
B =pd.DataFrame(np.array([veh,b]).T)
pd.concat([A,B],axis = 1, keys = [0])

答案 4 :(得分:0)

您可以从lookup_dfnon_luxury的列表中创建luxury

lookup_df = pd.DataFrame({
    'vehicle_make': luxury + non_luxury,
    'vehicl_make_category': (["luxury"] * len(luxury))+(["non_luxury"] * len(non_luxury))
})

然后left join在您拥有的原始df上。

df.merge(lookup_df, how='left',left_on='vehicle_make', right_on='vehicle_make') 

输出:

    vehicle_make    vehicle_model   vehicle_year    vehicle_make_category
0   Toyota               Corolla      2016             non_luxury
1   Hyundai               Sonata      2016             non_luxury
2   Cadillac               DTS        2006               NaN
3   Toyota                Prius       2014             non_luxury
4   Kia                   Optima      2015             non_luxury