2017-03-08 89 views
0

如果一列中的值在其他列中的兩值之間,我無法將權重(int)添加到新的Pandas DataFrame列列。不過,我可以用True/False值創建列(或者如果我使用astype,則爲0/1值)。如果1列中的值介於2個其他列中的值之間,則用重量創建Pandas DataFrame列

import pandas as pd 

df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6], 'c': [3,6,4]}) 
df 

    a b c 
0 1 4 3 
1 2 5 6 
2 3 6 4 

This works:

df['between_bool'] = df['c'].between(df['a'], df['b']) 
df 

    a b c between_bool 
0 1 4 3   True  # 3 is between 1 and 4 
1 2 5 6  False  # 6 is NOT between 2 and 5 
2 3 6 4   True  # 4 is between 3 and 6 

However, this does NOT work:

df['between_int'] = df['c'].apply(lambda x: 2 if df['c'].between(df['a'], df['b']) else 0) 

The code above generates the following error:

Traceback (most recent call last): 
    File "C:\Python36\envs\PortfolioManager\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code 
    exec(code_obj, self.user_global_ns, self.user_ns) 
    File "<ipython-input-14-0aa1e7cfd5c2>", line 1, in <module> 
    df['between_int'] = df['c'].apply(lambda x: 2 if df['c'].between(df['a'], df['b']) else 0) 
    File "C:\Python36\envs\PortfolioManager\lib\site-packages\pandas\core\series.py", line 2294, in apply 
    mapped = lib.map_infer(values, f, convert=convert_dtype) 
    File "pandas\src\inference.pyx", line 1207, in pandas.lib.map_infer (pandas\lib.c:66124) 
    File "<ipython-input-14-0aa1e7cfd5c2>", line 1, in <lambda> 

The desired output is:

a b c between_int 
0 1 4 3   2  # 3 is between 1 and 4 
1 2 5 6   0  # 6 is NOT between 2 and 5 
2 3 6 4   2  # 4 is between 3 and 6 

任何想法?

回答

1

我希望我理解正確的話,但如果你只是要添加的固定重量2在此條件下,一種選擇是做到以下幾點:

import numpy as np 
df['between_int'] = np.where(df['c'].between(df['a'], df['b']), 2, 0) 

或者,如果你這樣做,你可以做到以下幾點不想導入numpy:

df['between_int'] = 0 
df.loc[df['c'].between(df['a'], df['b']), 'between_int'] = 2 

希望這有助於!

+0

這正是我想要的。謝謝。 – vlmercado

相關問題