Home > Mobile >  python concatenating a list without restarting
python concatenating a list without restarting

Time:05-06

I'm trying to concatenate new rows of data into an old main dataframe. However, whenever I try to run my for loop. My old main dataframe restarts that's because of the exfive = pd.DataFrame() however without it. The Dataframe wouldn't have even been created. Also, if I hash it. The output gives me double the concatenated rows. So how do I go about being able to concatenate new rows of data without restarting or doubling my main?

nums_list = [2000,5500]
nums_list_two = [8000, 9000]

for num_list, num_list_two in zip(nums_list, nums_list_two):
    ex5 = pd.DataFrame({'colone':nums_list,'coltwo':nums_list_two})
    ex6 = pd.DataFrame({'colones':nums_list_two,'coltwos':nums_list})
    exs_list = [ex5, ex6]
        
    ex_mod = []
    for ex_list in exs_list:
        ex_list = ex_list.T
        ex_mod.append(ex_list)
            
    ex5 = ex_mod[0]
    ex6 = ex_mod[1]
    exfive = pd.DataFrame()
    exfive = pd.concat([exfive, ex5], ignore_index = False, axis = 0)
    del ex5

desired output:

          0      1
colone  1000    4500
coltwo  7000    8000
colone  2000    5500
coltwo  6000    9000

What I get when I have the pd.Dataframe not hashed:

          0      1
colone  2000    5500
coltwo  6000    9000

What I get when I have the pd.Dataframe hashed:

          0      1
colone  1000    4500
coltwo  7000    8000
colone  2000    5500
coltwo  6000    9000
colone  2000    5500
coltwo  6000    9000

I've tried this:

nums_list = [2000,5500]
nums_list_two = [8000, 9000]
exfive = pd.DataFrame()

for num_list, num_list_two in zip(nums_list, nums_list_two):
    ex5 = pd.DataFrame({'colone':nums_list,'coltwo':nums_list_two})
    ex6 = pd.DataFrame({'colones':nums_list_two,'coltwos':nums_list})
    exs_list = [ex5, ex6]

    ex_mod = []
    for ex_list in exs_list:
        ex_list = ex_list.T
        ex_mod.append(ex_list)

    ex5 = ex_mod[0]
    ex6 = ex_mod[1]
    exfive = exfive.append(ex5)

I don't get the desired outcome:

          0       1
colone  1000    4500
coltwo  7000    8000
colone  1000    4500
coltwo  7000    8000

I've also put the concat outside the for loop below it.

finalexfive = pd.concat(exfive, ignore_index = False, axis = 0)

But I get the outcome:

TypeError: first argument must be an iterable of pandas objects, you passed an object of type "DataFrame"

CodePudding user response:

Simple answer

Say you have df1 and you create df2 with the new list, provided they have the same column names, try doing df = pd.DataFrame(df1, df2)

Do tell if this worked.

CodePudding user response:

Try doing this

df = pd.DataFrame([[1, 2], [3, 4]], columns = ["a", "b"])
print(df)
to_append = [5, 6]
a_series = pd.Series(to_append, index = df.columns)
df = df.append(a_series, ignore_index=True)

This might also work. Basically you are trying to add new rows to the existing dataframe.

  • Related