Pandas:如何在 Dataframe 中创建新列并在考虑其他现有列的情况下在其中添加值

Pandas: How to create a new column in a Dataframe and add values in it considering other existing columns

我有一个代表一些餐馆及其名称的数据框。

data = {
        'restaurant_id':  ['1', '2','3','4','5','6','7','8','9','10','11','12'],
        'restaurant_name':  ['Dennys', 'Dennys','Pho U','Pho U','Dennys','Japanese Cafe','Japanese Cafe','Midori','Midori','xxx','yyy','zzz'],
        }

df = pd.DataFrame (data, columns = ['restaurant_id','restaurant_name'])

df.head(15)

例如,xxxyyyzzz 不是链的一部分。

我不确定使用 pandas 实现类似目标的正确语法。如果需要任何说明,请询问。

谢谢。

这听起来像 duplicated:

 df['is_chain'] = df['restaurant_name'].duplicated(keep=False).astype(int)

输出:

   restaurant_id restaurant_name  is_chain
0              1          Dennys         1
1              2          Dennys         1
2              3           Pho U         1
3              4           Pho U         1
4              5          Dennys         1
5              6   Japanese Cafe         1
6              7   Japanese Cafe         1
7              8          Midori         1
8              9          Midori         1
9             10             xxx         0
10            11             yyy         0
11            12             zzz         0