1

I want to assign a value to the variable based on the existing variable values. I will give you a brief explanation of the data.

mydata

      category  original_wt  predicted_wt   categorized
    1 xxxxx      2.5          3.0            original
    2 yyyyy      3.5          4.0            predicted
    3 zzzzz      3.0          5.0            predicted
    4 aaaaa      4.0          2.5            original
    5 bbbbb      3.2          5.5            original
    6 ccccc      4.6          3.5            predicted

From the above data i want to assign a value based on categorized type.
If it is original the categorized value should be original_wt of same row.
If it is predicted the categorized value should be predicted_wt of same row.

Expected output:

mydata

  category  original_wt  predicted_wt   categorized   categorized_value
1 xxxxx      2.5          3.0            original      2.5
2 yyyyy      3.5          4.0            predicted     4.0
3 zzzzz      3.0          5.0            predicted     5.0
4 aaaaa      4.0          2.5            original      4.0
5 bbbbb      3.2          5.5            original      3.2
6 ccccc      4.6          3.5            predicted     3.5

Tried:

mydata['categorized_value'] = 
     if mydata['categorized'] == 'original':
        mydata['categorized_value'] = mydata['original_wt']
     else:
     mydata['categorized_value'] = mydata['predicted_wt']

How do i get my expected output in python?

4
  • 1
    Is the code indented correctly? The else: branch is not indented now and will always be run, so value is always predicted Commented May 14, 2015 at 9:20
  • What output do you get? Commented May 14, 2015 at 9:21
  • @Tichodroma invalid syntax! thanks for showing interest. Commented May 14, 2015 at 9:32
  • @SamiKuhmonen indented correctly in terminal but not here Commented May 14, 2015 at 9:33

2 Answers 2

3

Use loc and a boolean mask to set the values you desire:

In [221]:

df.loc[df['categorized'] == 'original', 'categorized_value'] = df['original_wt']
df.loc[df['categorized'] == 'predicted', 'categorized_value'] = df['predicted_wt']
df
Out[221]:
  category  original_wt  predicted_wt categorized  categorized_value
1    xxxxx          2.5           3.0    original                2.5
2    yyyyy          3.5           4.0   predicted                4.0
3    zzzzz          3.0           5.0   predicted                5.0
4    aaaaa          4.0           2.5    original                4.0
5    bbbbb          3.2           5.5    original                3.2
6    ccccc          4.6           3.5   predicted                3.5
Sign up to request clarification or add additional context in comments.

Comments

1

You can use apply:

mydata['categorized_value'] = mydata.apply(lambda x: x['original_wt'] if x['categorized'] == 'original' else x['predicted_wt'], axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.