1

I have a column in my data frame which similar to:

type
  
phone
smartphone
handphone
handphone

and I want to change the first "handphone" to phone, and the second "handphone" to smartphone. How can I make it in python? So far, what I have done was using df.replace(regex) but it changes both "handphone" as phone, while what I expect is:

type

phone
smartphone
phone
smartphone

thankyou.

5
  • Where is the attempted approach? Commented Aug 28, 2020 at 17:07
  • How do you choose which one to change to phone and which one to change to smartphone? Commented Aug 28, 2020 at 17:07
  • What if there are more than two occurrence ? Commented Aug 28, 2020 at 17:09
  • @Onyambu I have set the column, so the first handphone will always be phone and the second phone is smartphone Commented Aug 28, 2020 at 17:18
  • @sushanth I have minimalized the column so there will be only two occurrence Commented Aug 28, 2020 at 17:19

4 Answers 4

3

TL;DR

df[df['type'] == 'handphone'] = [['phone'], ['smartphone']]

Why does this work?

We are slicing the dataframe to only return cells where they are equal to 'handphone' in the column 'type'. Then, we reassign those cells to be 'phone' and 'smartphone', in order.

Breaking it down

The first part of the solution:

df[df['type'] == 'handphone']

Prints:

[['handphone'], ['handphone']] 

And then when we reassign it with:

df[df['type'] == 'handphone'] = [['phone'], ['smartphone']]

So it becomes this:

[['phone'], ['smartphone']]
Sign up to request clarification or add additional context in comments.

2 Comments

or, df.loc[df.type.eq('handphone'), 'type'] = ['phone', 'smartphone']
@sushanth Yeah, although this actually might take longer to run because of the added search for type.
1

Assuming you can make this a list, here is a simple solution that should work:

lists = ["phone", "smartphone", "handphone", "handphone"]
changelist = ["phone", "smartphone"]

num = 0
for row in range(len(lists)):
    if lists[row] == "handphone":
        lists[row] = changelist[num]
        num += 1

print(lists)

>>>['phone', 'smartphone', 'phone', 'smartphone']

Comments

0

Here is how:

import pandas

df = pandas.Series(['phone', 'smartphone', 'handphone', 'handphone'])
df[df[df == 'handphone'].index[0]] = 'phone' # Find the first occurrence of 'handphone' and replace it with 'phone'
df[df[df == 'handphone'].index[0]] = 'smatphone' # Find the first occurrence of 'handphone' again, but now it will be the equivalent of the second occurrence, and replace it with 'smartphone'

print(df)

Output:

0         phone
1    smartphone
2         phone
3     smatphone
dtype: object

Comments

0
import pandas as pd

df = pd.read_csv("File.csv") # File.csv have your given data in question

my_type = df["type"].tolist() # Series to list

index_phone = my_type.index("handphone")
my_type[index_phone] = "phone"

index_phone = my_type.index("handphone")
my_type[index_phone] = "smartphone"

my_type

Output:

['phone', 'smartphone', 'phone', 'smartphone']

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.