Python ValueError来自np.where,根据一个条件创建标志

2024-09-26 22:52:40 发布

您现在位置:Python中文网/ 问答频道 /正文

如果在cities_specific中提到了城市,我想在cities_all数据中创建一个标志。这只是一个很小的例子,实际上我想基于多个数据帧创建多个标志。这就是为什么我试图用isin而不是join来解决它

然而,我遇到了ValueError: Length of values (3) does not match length of index (7)

# import packages
import pandas as pd
import numpy as np


# create minimal data 
cities_specific = pd.DataFrame({'city': ['Melbourne', 'Cairns', 'Sydney'],
                       'n': [10, 4, 8]})
cities_all = pd.DataFrame({'city': ['Vancouver', 'Melbourne', 'Athen', 'Vienna', 'Cairns',
                                    'Berlin', 'Sydney'],
                          'inhabitants': [675218, 5000000, 664046, 1897000, 150041, 3769000, 5312000]})


# get value error
# how can this be solved differently?
cities_all.assign(in_cities_specific=np.where(cities_specific.city.isin(cities_all.city), '1', '0'))


# that's the solution I would like to get
expected_solution = pd.DataFrame({'city': ['Vancouver', 'Melbourne', 'Athen', 'Vienna', 'Cairns',
                                    'Berlin', 'Sydney'],
                          'inhabitants': [675218, 5000000, 664046, 1897000, 150041, 3769000, 5312000],
                          'in_cities': [0, 1, 0, 0, 1, 0, 1]})

Tags: of数据importcitydataframe标志asall
1条回答
网友
1楼 · 发布于 2024-09-26 22:52:40

我认为你在这种情况下改变了立场。 这里有一些备选方案:

cities_all.assign(
     in_cities_specific=np.where(cities_all.city.isin(cities_specific.city), '1', '0')
)

cities_all["in_cities_specific"] =  
                    cities_all["city"].isin(cities_specific["city"]).astype(int).astype(str)

condlist = [cities_all["city"].isin(cities_specific["city"])]
choicelist = ["1"]
cities_all["in_cities_specific"] = np.select(condlist, choicelist,default="0")

相关问题 更多 >

    热门问题