我想将数据集的两个作业重命名为“ pastry”。我创建了一个词典,将新名称作为关键字,并将以前的类别作为列表
# dataframe for artificial dataframe
salary = [100, 200, 125, 400, 200]
job = ["pastry Commis ", "line cook", "pastry Commis", "pastry chef", "line cook"]
# New categories
cat_ac = {"pastry": ["pastry Commis", "pastry chef"]}
df_test = pd.DataFrame({"salary": salary, "job": job})
df_test.head()
然后
df_test.loc[df_test["job"].isin(cat_ac[list(cat_ac.keys())[0]]), "job"] = list(cat_ac.keys())[0]
df_test
在这个小的数据集上一切正常,但是当我对我的4万行数据进行相同的实验时,与以下任务“ pastry Comis”和“ pastry Chef”相对应的所有行都消失了。或新的类别“糕点”
# We read the lines with the new category
df.loc[df["job"].isin(["pastry"]), "job"]
Out: Series([], Name: job, dtype: object)
# We read the lines with the previous categories
df.loc[df["job"].isin(cat_baking[list(cat_baking.keys())[0]]), "job"]
Out: Series([], Name: job, dtype: object)
有什么问题的想法吗?
答案 0 :(得分:2)
您可以使用:
df_test.job.replace({i:k for i in v for k, v in cat_ac.items()})
0 pastry Commis
1 line cook
2 pastry
3 pastry
4 line cook
注意 :我认为您已经为第一条记录留出了空间,因此它不会替代原先打算的记录,因为您的工作解决方案也是如此,我们可以使用str.strip()
来处理他们
答案 1 :(得分:1)
使用您的import * as firebase from 'firebase';
import flamelink from 'flamelink';
const firebaseConfig = {
apiKey: '<your-api-key>', // required
authDomain: '<your-auth-domain>', // required
databaseURL: '<your-database-url>', // required
projectId: '<your-project-id>', // required
storageBucket: '<your-storage-bucket-code>', // required
messagingSenderId: '<your-messenger-id>' // optional
};
const firebaseApp = firebase.initializeApp(firebaseConfig);
const app = flamelink({ firebaseApp });
替换用正则表达式模式替换:
dict
答案 2 :(得分:0)
您也可以使用np.where:
import numpy as np
df_test['job'] = np.where((df_test['job'].str.contains('pastry Commis')) | (df_test['job'].str.contains('pastry chef')), 'pastry', df_test['job'])