0

I have a dataframe that ask for the Unique value and I will like to get another column based on those unique value.

Trying to get the colour in all_names

raw_data={'Class':['A1','B1','C1','D1','A1'],
          'Name':['Harry','Christabel','Mel','Chris','Cherry'],
          'Color':['Red','Blue','Pink','Red','Red']}

df = pd.DataFrame(raw_data)
all_names = df['Name'][df['Class']=='A1'].unique()
all_colour=df.loc[df['Colour'].isin(all_names)]

I have tried the code above but I am trying to get the color for Harry and Cherry which is red and red.

1
  • The dictionary was missing some but it has been well understood. Commented Aug 12, 2019 at 23:53

2 Answers 2

1

1 If you simply want the color of Harry and Cherry do:

Color_Harry_Cherry=list(df.loc[[0,4],'Color'])
print(Color_Harry_Cherry)

Out:

['Red', 'Red']

2 If you want to get the colors of the list all_names you could do this to do it without errors:

import numpy as np
import pandas as pd
raw_data={'Class':['A1','B1','C1','D1','A1'],'Name':['Harry','Christabel','Mel','Chris','Cherry'],'Color':['Red','Blue','Pink','Red','Red']}
df = pd.DataFrame(raw_data)
all_names = df['Name'][df['Class']=='A1'].unique()
print(all_names)
i=0
colors = []
while i<len(df['Color']):
    if df.loc[df.index.values[i],'Name'] in all_names:
        colors = colors + list(df.loc[[df.index.values[i]],'Color'])
    i+=1
print(colors)

Out:

['Harry' 'Cherry']
['Red', 'Red']

3 Why didn't your code work?

Notice that df['Color'].isin(all_names) returns:

0    False
1    False
2    False
3    False
4    False
Name: Color, dtype: bool

And the loc method, needs as arguments the index and the column where the value you want to select from the DataFrame is located. Because of this you received an error.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like your dataset is not in correct dictionary format. It should look like this :

raw_data={
    'Class':['A1','B1','C1','D1','A1'],
    'Name':['Harry','Christabel','Mel','Chris','Cherry'],
    'Color':['Red','Blue','Pink','Red','Red']
}

After that, you can continue subsetting your columns. Check this code :

import pandas as pd

# Your dataset should look like this
raw_data={
    'Class':['A1','B1','C1','D1','A1'],
    'Name':['Harry','Christabel','Mel','Chris','Cherry'],
    'Color':['Red','Blue','Pink','Red','Red']
}

# Assign dataset into pandas dataframe
df = pd.DataFrame(raw_data)

# Get Harry's color
harry = df.loc[df["Name"] == "Harry", "Color"].unique()[0]

# Get Cherry's color
cherry = df.loc[df["Name"] == "Cherry", "Color"].unique()[0]

# Print them
print("Harry's color is %s" % harry)
print("Cherry's color is %s" % cherry)

That should work.

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.