1

I have 2 snippets which are giving me different outputs. The first snippet gives me the desired output but I want to replicate the same output with the 2nd snippet.

Snippet 1:

Top= pd.DataFrame({'A':['Hello', 'World']})

insrt1= pd.DataFrame({'A': [f'Appended Item-1x']})

df=Top['A'].append(insrt1['A'])
print(df)

Out:

0               Hello
1               World
0    Appended Item-1x
Name: A, dtype: object

The 2nd snippet yields a different output due to the for loop. How can I achieve the same output as in snippet 1?

Snippet2

Top= pd.DataFrame({'A':['Hello', 'World']})

Frst=['1','2']
Scnd=['x','y']

d={}

for num1 in Frst:
    for num2 in Scnd:
        key = f'-OE{num1}{num2}-'
        insrt2 = pd.DataFrame({'A': [f'Appended Item-{num1}{num2}']})
        d[key] = insrt2

df=Top['A'].append(d['-OE1x-'])
print(df)

Out:

     0                 A
0  Hello               NaN
1  World               NaN
0    NaN  Appended Item-1x
5
  • Could you please provide an example of expected/desired output? Commented Oct 5, 2020 at 12:56
  • Desired output is that shown from snippet 1. I want to replicate it with snippet 2 Commented Oct 5, 2020 at 12:59
  • Are you looking to append rows where column A has values Appended Item-1x, ...1y, ...2x, ...2y? Please be specific when asking others for help. Commented Oct 5, 2020 at 13:04
  • No. I would like just 1 column with 3 rows consisting of ‘Hello’ first entry , ‘World’ second entry and ‘ Appended Item-1x’ third entry with name of column being ‘A’ Commented Oct 5, 2020 at 13:22
  • I see, putting an answer together for you now. Commented Oct 5, 2020 at 13:27

2 Answers 2

1

The issue that's causing undesired output is coming from the fact that you're attempting to append a full Pandas.DataFrame to a Pandas.Series with the statement

df=Top['A'].append(d['-OE1x-'])

If you change this line to:

df = Top.append(d['-OE1x-'])

df will look like this:

                  A
0             Hello
1             World
0  Appended Item-1x

You may want to pass ignore_index=True as an argument to your call to Pandas.DataFrame.append() so that the row containing Appended Item-1x is given a sequential index - i.e the original index of 0 is not included as this would result in two rows with index 0 in df.

e.g.

df = Top.append(d['-OE1x-'], ignore_index=True)

Will give you the following df:

                  A
0             Hello
1             World
2  Appended Item-1x

Alternative Solution

Since it seems that you don't actually make use of each Pandas.DataFrame in d outside of appending them as new rows to existing dataframes, it may be a good idea to refactor your code so that each entry in d looks like str: str instead of str: Pandas.DataFrame. Using your original code you could achieve this as follows:

import pandas as pd

Top = pd.DataFrame({'A':['Hello', 'World']})

Frst = ['1','2']
Scnd = ['x','y']

d = { f'-OE{num1}{num2}-': f'Appended Item-{num1}{num2}' for num1 in Frst for num2 in Scnd }

df = Top.append({'A': d['-OE1x-']}, ignore_index=True)

This will also provide the desired df:

                  A
0             Hello
1             World
2  Appended Item-1x

However unlike the previous answer provided (and your original code) it will be much less memory intensive as d is not being filled unnecessarily with instances of Pandas.DataFrame.

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

3 Comments

@JP193. Both answers you provide work for me. Now I have come across another problem, details of which I did not include in the original question. It is essentially the appending of another dataframe of numbers after 'Append Item-1x'.
here is a snippet of my real problem 'df=TOP.append({'A':d['-OE1x-']},ignore_index=True).append (df.loc[0].append more.............`
the error is cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid
0

I think just convert those lists to series and append should do it.

Code:

import pandas as pd
Top= pd.DataFrame({'A':['Hello', 'World']})
Frst=['1','2']
Scnd=['x','y']
l = (pd.Series(Frst), pd.Series(Scnd))
df = Top.A
for i in l:
    df = df.append(i)
print(df)

Ouput:

0    Hello
1    World
0        1
1        2
0        x
1        y
dtype: object

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.