简体   繁体   English

使用numpy.where向数据框输入值

[英]Entering values to dataframe with numpy.where

I'm trying to entering values to dataframe based on a condition. 我正在尝试根据条件向数据框输入值。 I have movie attribute columns like 'AttributeHorror' or 'AttributeRomance' etc. Columns are filled with NaN. 我有电影属性列,例如'AttributeHorror'或'AttributeRomance'等。这些列用NaN填充。 If the condition is met, it have to replace with 1. if not, 0. 如果满足条件,则必须替换为1 .;否则,请替换为0。

I tried the following code but it didnt return correct. 我尝试了以下代码,但未返回正确的代码。


film_data['AttributeOlumsuz'] 
       =np.where(film_data['FilmAttributes1']=='Olumsuz', '1', '0')


film_data['AttributeSiddet'] = np.where(film_data['FilmAttributes1']=='Siddet', '1', '0')


film_data['AttributeAltyazi'] = 
np.where(film_data['FilmAttributes1']=='Altyazili', '1', '0')

film_data['AttributeGerilim'] = 
np.where(film_data['FilmAttributes1']=='Gerilim', '1', '0')

This code fills every cell incorrect. 此代码填充每个错误的单元格。 Where am i making mistake? 我在哪里犯错?

A simple list comprehension should work. 一个简单的列表理解应该起作用。

There are two parts: 有两个部分:

1) iterate over the original values 1)迭代原始值

2) list comprehension conditional assignment 2)列表理解条件分配

These are commented in the code below. 这些在下面的代码中进行了注释。

film_data['AttributeOlumsuz'] = [
    '1' if v=='olumsuz' else '0' #2 list comprehension conditional assignment here
    for v in film_data['FilmAttributes1'] #1 iterate over original values here
]

If numpy.where() does not work. 如果numpy.where()不起作用。

Assuming you are reading your data using pandas.DataFrame, you can try: 假设您正在使用pandas.DataFrame读取数据,则可以尝试:

First Possible Solution: 第一个可能的解决方案:

df.loc[df['AttributeOlumsuz'] == 'Olumsuz', 'Tagger_Column'] = '1'

Second Possible Solution: 第二种可能的解决方案:

You can also use .map() method. 您也可以使用.map()方法。

your_dict = {'Olumsuz': '1'}
df['Tagger_Column'] = df['AttributeOlumsuz'].map(your_dict)

Result of map for False entries is NaN , if you don't like that you can use fillna with parameter equals to whatever value you please: False条目的map结果为NaN ,如果您不喜欢,则可以将fillna与参数等于您想要的任何值一起使用:

df['Tagger_Column'].fillna(value = '0') # to replicate your condition in numpy.where() above

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM