gpt4 book ai didi

python - 如何使用其他现有类别覆盖 pandas DataFrames 中的列

转载 作者:行者123 更新时间:2023-12-01 09:15:44 25 4
gpt4 key购买 nike

这是我的数据集

No   Item             Desc
1 AWS Lambda
2 AWS Polly
3 Microsoft Azure
4 Microsoft Excel
5 Google BigQuery
6 RDS Database
7 Athena Big Data
8 Lambda NoSQL

我想要的是将 AWSGoogleMicrosoft 之外的任何其他项目制作为 AWS

No   Item             Desc
1 AWS Lambda
2 AWS Polly
3 Microsoft Azure
4 Microsoft Excel
5 Google BigQuery
6 AWS Database
7 AWS Big Data
8 AWS NoSQL

最佳答案

使用numpy.whereisin :

df['Item'] = np.where(df['Item'].isin(['Microsoft','Google']), df['Item'], 'AWS')
#alternative
#df['Item'] = np.where(np.in1d(df['Item'], ['Microsoft','Google']), df['Item'], 'AWS')

或者:

df.loc[~df['Item'].isin(['Microsoft','Google']), 'Item'] = 'AWS'
<小时/>
print (df)
No Item Desc
0 1 AWS Lambda
1 2 AWS Polly
2 3 Microsoft Azure
3 4 Microsoft Excel
4 5 Google BigQuery
5 6 AWS Database
6 7 AWS Big Data
7 8 AWS NoSQL

关于python - 如何使用其他现有类别覆盖 pandas DataFrames 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51280076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com