-3

I'm trying to write a function which takes a dataframe, duplicates it, changes the values in one of the columns and then concatenates them together. For example:

df1 looks like

column1 | column2 | column3
keyword1|    1    |   20
keyword2|    1    |   40
keyword3|    1    |   60

and I want a function which creates this:

column1 | column2 | column3
keyword1|    1    |   20
keyword2|    1    |   40
keyword3|    1    |   60
keyword1|    2    |   20
keyword2|    2    |   40
keyword3|    2    |   60

I've tried this:

def add_mobile(df):

    mobiledf = df.copy(deep=True)
    mobiledf['column2'].replace(1,2,inplace=True)

    df = pd.concat([df,mobiledf],axis=0)

however when I put df1 though this function it returns df1 unedited. If I then look at df1.shape it has not changed.

jceg316
  • 469
  • 1
  • 9
  • 17
  • Can you try return df below the function? Because I have tried your code and it worked for me. – demirbilek Jun 08 '18 at 13:25
  • The second function just returns the copied and modified `df`. Do you assign it to variable and perform the concat afterwards? – Fourier Jun 08 '18 at 13:25
  • The second function is there to show that the second DF is being made with no problems. The first function does concatenate them. – jceg316 Jun 08 '18 at 13:26
  • You are not returning your df in first function and the second function does not concat the dataframes. – harvpan Jun 08 '18 at 13:29
  • If I put a return statement in my function it works, however when I add df1 as a parameter it doesn't actually edit df1. – jceg316 Jun 08 '18 at 13:29

2 Answers2

1

enter image description hereThis worked for me:

def add_mobile(df):
   mobiledf = df.copy(deep=True)
   mobiledf['column2'].replace(1,2,inplace=True)
   df = pd.concat([df,mobiledf],axis=0,ignore_index=True)
   return df
demirbilek
  • 331
  • 1
  • 11
1

First function

You didn't return df so it didn't do anything:

def add_mobile(df):

    mobiledf = df.copy(deep=True)
    mobiledf['column2'].replace(1,2,inplace=True)

    df = pd.concat([df,mobiledf],axis=0)

    return df

Second function:

You didn't concatenate:

def add_mobile(df):

    mobiledf = df.copy(deep=True)
    mobiledf['column2'].replace(1,2,inplace=True)

    return pd.concat([df,mobiledf],axis=0)

Concatenate is not an in-place function

Finlay, concatenate is not an in-place function, so you should always retrieve the result:

df = add_mobile(df)
Emmanuel-Lin
  • 1,848
  • 1
  • 16
  • 31
  • This function works. But when I look at `df1.shape` it remains the same and does not have the other df concatenated. It's as if there's an `inplace=True` missing or something – jceg316 Jun 08 '18 at 13:35
  • 1
    Yes, this is because concat is not an inplace function. Just perform `df1 = add_mobile(df1)` – Emmanuel-Lin Jun 08 '18 at 13:42
  • Are have a look at this [question about concatenating without copy](https://stackoverflow.com/questions/7869095/concatenate-numpy-arrays-without-copying) – Emmanuel-Lin Jun 08 '18 at 13:43