0

I wrote a code that takes value in cell, changes it and the it should replace value in that cell with new value. I have .replace function fit dummy dataframe, and it works, but for my example it does not work. Old value and new value are very long strings. I have tried with inplace=True and without it.

My goal is to change ServiceDirection value.

You can download the data from here:

https://www.sendspace.com/file/7soufd

old value looks like this: ...ype=1|ServiceDirection=2|CmtsMdIfIn...

new value looks like this: ype=1|ServiceDirection=DS|CmtsMdIfIn

This is the code:

data = pd.read_csv('data.csv')

def third_task():

    new_data = data

    for column in data:

        for row in data[column]:

            if 'ServiceDirection=1' in str(row):

                new_row = str(row).replace('ServiceDirection=1', 'ServiceDirection=DS')
                new_data = data.replace(str(row), new_row)

            elif 'ServiceDirection=2' in str(row):

                new_row = str(row).replace('ServiceDirection=2', 'ServiceDirection=US')
                new_data = data.replace(str(row), new_row)


    export_csv = new_data.to_csv(r'C:\Users\Pc\Desktop\export_dataframe1.csv', index = None, header=False)

    return new_data

print(third_task())

I have also tried to do this:

df.replace(row, result)

Instead of this: data[column] = data[column].replace(str(row), str(result), inplace=True)

But still does not work, it always returns dataframe with old values

6
  • 1
    Can you give a minimal-reproducible-example ? Commented Jun 28, 2019 at 22:32
  • @Terry I updated the question, you have the database in there Commented Jun 28, 2019 at 22:43
  • I see problem when I read to for row in data['column'] Commented Jun 28, 2019 at 23:00
  • Whats the problem? @QuangHoang Commented Jun 28, 2019 at 23:02
  • 1
    your .csv is so messy, What are you trying to do with this data? Commented Jun 28, 2019 at 23:04

1 Answer 1

1

I do not know if you are trying to structure your data. If so, I did it this way.

df = pd.read_csv('data.csv', sep = '|', header = None)
df.columns = df.iloc[0, :].apply(lambda x: x.split('=')[0])
df = df.apply(lambda x: x.str.split('=').str.get(1))
df.head()

    ServiceSlaDelayPkts ServiceTimeCreated  CmtsMdIfName    ServiceSlaDropPkts  ServiceGateId   ServiceClassName    CmtsSysUpTime   ServicePktsPassed   ServiceIdentifier   ServiceDsMulticast  ... ServiceTimeActive   CmMacAddr   ServiceOctetsPassed ServiceAppId    CmtsHostName    RecCreationTime RecType ServiceDirection    CmtsMdIfIndex   ,,,
0   0   4199286300  Cable1/0/0  0   0   USXnet  4294746100  7710    13  0   ... 954374  aaaa.bbbb.cccc  1033134 7   ibis-instruments-1.com  1555675968867   1   2   1001    NaN
1   0   4199286300  Cable0/0/0  0   0   DSXnet  4294746100  287 14  0   ... 954374  aaaa.bbbb.cccc  96868   7   ibis-instruments-1.com  1555675968867   1   1   1001    NaN
2   0   4199290300  Cable1/0/0  0   0   USXnet  4294746100  9527731 15  0   ... 954284  dddd.bbbb.cccc  1471545334  7   ibis-instruments-1.com  1555675968867   1   2   1001    NaN
3   0   4199290300  Cable0/0/0  0   0   DSXnet  4294746100  128871002   16  0   ... 2968    dddd.bbbb.cccc  188935852314    7   ibis-instruments-1.com  1555675968867   1   1   1001    NaN
4   0   4260449700  Cable0/0/0  0   0   USXnet  4294746100  452297  17  0   ... 342739  dddd.bbbb.mmmm  77459364    7   ibis-instruments-2.com  1555675968868   1   2   1001    NaN

Edit: Add dots and upper case on CmMacAddr column

df['CmMacAddr'] = df['CmMacAddr'].str.replace('.', '').str.upper()

df['CmMacAddr'] = df['CmMacAddr'].apply(lambda x: '.'.join(x[i:i+2] for i in range(0,len(x), 2)))

Explanation

The first line of code read the .csv separating the columns by '|' (the default is comma(,)).
The second line i rename the columns names, because your csv dont have header, i selected only one row (df.iloc[0, :]) and then I cover all values(apply) spliting by '=' and taking the values from index 0.
The last line of code is very similar to the second, but i take se second value from each split and replace all rows with this new value.

If you execute row by row my code and adding df.head() between them you will see the evolution :)

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

4 Comments

CmMacAddr column is like this dddd.bbbb.mmmm, and needs to be like this DD.DD.BB.BB.MM.MM Can you please update your answer and explain me this df.columns = df.iloc[0, :].apply(lambda x: x.split('=')[0]) df = df.apply(lambda x: x.str.split('=').str.get(1)) Your answer is amazing, but I do not know why did you do this?
Your answer is amazing, but my data needs to have the same structure like on the start.
As I said, your answer is amazing , and Ill put it as a correct, but If you can please help me without changing the data structure, because thats how I need to solve this. Thanks
I do not know how to do it that way mate. I hope someone shows up with the correct answer

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.