Home > Blockchain >  Dataframe(pandas) append not working in a loop
Dataframe(pandas) append not working in a loop

Time:08-13

I am trying to append the DataFrame into existing DataFrame using loop. Currently, new_data has 4 values each column. I want to go through loop and add new data which is df2 with the 3 values each column every time loop iterates.

 new_data = df = pd.DataFrame({"a":[1, 2, 3, 4],
                             "b":[5, 6, 7, 8]})

for i in range(len(5)):
    df2 = pd.DataFrame({"a":[1, 2, 3],
                    "b":[5, 6, 7]})
    print(df2)
    new_data.append(df2)

The final result should have 19 values each column,for example

a  b
----
1  5
2  6
3  7
4  8
1  5
2  6
3  7
1  5
2  6
3  7
1  5
2  6
3  7
1  5
2  6
3  7
1  5
2  6
3  7

But for some reason it's not working and I am confused. When I try to perform the operation without a loop it is working properly.

For example:

# Creating the first Dataframe using dictionary
df1 = df = pd.DataFrame({"a":[1, 2, 3, 4],
                         "b":[5, 6, 7, 8]})
  
# Creating the Second Dataframe using dictionary
df2 = pd.DataFrame({"a":[1, 2, 3],
                    "b":[5, 6, 7]})
  
# Print df1
print(df1, "\n")
df1.append(df2)

I don't understand what the issue is here. Please explain to me what the issue is here.

CodePudding user response:

You need to:

df1 = df1.append(df2)

And even better, don't use append which will be deprecated soon and use concat instead:

df1 = pd.concat([df1, df2])

CodePudding user response:

Instead of doing loop, you can use pd.concat to replicate your dataframe df2 according to your desired times. Only then, you join both dataframe together.

replicate = 5

new_df2 = pd.concat([df2]*replicate)

pd.concat([new_data, new_df2], ignore_index=True)
Out[34]: 
    a  b
0   1  5
1   2  6
2   3  7
3   4  8
4   1  5
5   2  6
6   3  7
7   1  5
8   2  6
9   3  7
10  1  5
11  2  6
12  3  7
13  1  5
14  2  6
15  3  7
16  1  5
17  2  6
18  3  7
  • Related