python - 在python中为对象赋值
问题描述
我正在尝试将 1 和 0 分配给数据框中的列,该列实际上是一个字符串。在下面的日期集中,'abc' 为 1,其余为 0
以下是样本数据集:
type={'type':['abc','abc','kk','arr','yg','np']}
df=pd.DataFrame(type,columns=['type'])
我的代码:
df['new']=np.where(df['type']=='abd',1,0)
df['new'].value_counts()
输出:
0 6
Name: new, dtype: int64
解决方案
试试这个,而不是 np.where:
df['new'] = df['type'] == 'abc'
顺便说一句:你写的是 'abd' 而不是 'abc' 所以你有 6 - 0 :)
推荐阅读
- regex - 如何处理字符串中的自定义标签并记录有关格式的信息
- google-apps-script - 数据更改时自动对行进行排序
- r - 按日期和变量对数据框进行排序
- c# - 在未激活 .NET 3.5 Windows 功能的情况下运行 C# exe
- react-native - 如何在 react-native viewpager 中使用动画更改背景颜色?
- c# - 用粗体标签在短语周围附加单词
- javascript - 并行异步/等待操作
- apache - .htaccess -> Nginx 配置
- html - 有没有办法在 Internet Explorer 中显示 MJPEG 视频流?
- css - 我怎样才能只继承引导预定义类的几个属性?