Home > Blockchain >  How do you append a specific row from one data frame to another Data frame?
How do you append a specific row from one data frame to another Data frame?

Time:11-01

I have one data frame. I am sorting out particular values from it and want to input that row of data. In this case the time, which is the index, and the action of which is happening. I have the row number that I want to input however, when I try doing this,

a = a.append(data.values[i])

or

a = a.append(data.iloc[i])

I receive the error append() missing one required positional argument: 'other'

This may be a very simple problem. However, I am knew to this library and data structure, and looking for some insight.

CodePudding user response:

Try this:

a = pd.concat([a, data.iloc[i].to_frame().T], axis=0)

CodePudding user response:

df1 = pd.DataFrame({
    "foo": [1, 2, 3, 4],
    "spam": [5, 6, 7, 8]
})
print(f"{df1}\n")

df2 = pd.DataFrame({
    "foo": [11, 12, 13, 14],
    "spam": [20, 22, 24, 26]
})
print(f"{df2}\n")

df1.loc[df2.index[0]] = df2.iloc[0]
print(df1)

   foo  spam
0    1     5
1    2     6
2    3     7
3    4     8

   foo  spam
0   11    20
1   12    22
2   13    24
3   14    26

   foo  spam
0   11    20
1    2     6
2    3     7
3    4     8
  • Related