Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Dataframe(pandas) append not working in a loop

$
0
0

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  52  63  74  81  52  63  71  52  63  71  52  63  71  52  63  71  52  63  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 dictionarydf1 = df = pd.DataFrame({"a":[1, 2, 3, 4],"b":[5, 6, 7, 8]})# Creating the Second Dataframe using dictionarydf2 = pd.DataFrame({"a":[1, 2, 3],"b":[5, 6, 7]})# Print df1print(df1, "\n")df1.append(df2)

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


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>