Append list to pandas DataFrame as new row with index

2024/9/19 9:21:40

Despite of the numerous stack overflow questions on appending data to a dataframe I could not really find an answer to the following. I am looking for a straight forward solution to append a list as last row of a dataframe. Imagine I have a simple dataframe:

 indexlist=['one']columnList=list('ABC')values=np.array([1,2,3])# take care, the values array is a 3x1 size array. # row has to be 1x3 so we have to reshape itvalues=values.reshape(1,3)
df3=pd.DataFrame(values,index=indexlist,columns=columnList)
print(df3)A  B  C
one  1  2  3

After some operations I get the following list:

listtwo=[4,5,6]

I want to append it at the end of the dataframe. I change that list into a series:

oseries=pd.Series(listtwo)
print(type(oseries))
oseries.name="two"

now, this does not work:

df3.append(oseries)

since it gives:

A   B   C   0   1   2
one 1.0 2.0 3.0 NaN NaN NaN
two NaN NaN NaN 5.0 6.0 7.0

I would like to have the values under A B and C.

I also tried:

df3.append(oseries, columns=list('ABC'))  *** not working ***
df3.append(oseries, ignore_index=True)  *** working but wrong result
df3.append(oseries, ignore_index=False) *** working but wrong resultdf3.loc[oseries.name]=oseries adds a row with NaN values

what I am looking for is a) how can I add a list to a particular index name b) how can I simple add a row of values out of a list even if I don't have a name for index (leave it empty)

Answer

Either assign in-place with loc:

df.loc['two'] = [4, 5, 6]
# df.loc['two', :] = [4, 5, 6]
dfA  B  C
one  1  2  3
two  4  5  6

Or, use df.append with the second argument being a Series object having appropriate index and name:

s = pd.Series(dict(zip(df.columns, [4, 5, 6])).rename('two'))
df2 = df.append(s)df2A  B  C
one  1  2  3
two  4  5  6

If you are appending to a DataFrame without an index (i.e., having a numeric index), you can use loc after finding the max of the index and incrementing by 1:

df4 = pd.DataFrame(np.array([1,2,3]).reshape(1,3), columns=list('ABC'))
df4A  B  C
0  1  2  3df4.loc[df4.index.max() + 1, :] = [4, 5, 6]
df4A    B    C
0  1.0  2.0  3.0
1  4.0  5.0  6.0

Or, using append with ignore_index=True:

df4.append(pd.Series(dict(zip(df4.columns, [4, 5, 6]))), ignore_index=True)A  B  C
0  1  2  3
1  4  5  6
https://en.xdnf.cn/q/72601.html

Related Q&A

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that Ive captured wit…

Saving matplotlib subplot figure to image file

Im fairly new to matplotlib and am limping along. That said, I havent found an obvious answer to this question.I have a scatter plot I wanted colored by groups, and it looked like plotting via a loop w…

Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars

I have a 3 dimensional data set that I am trying to manipulate in the following way. data.shape = (643, 2890, 10) vector.shape = (643,)I would like numpy to see data as a 643 length 1-D array of 2890x1…

delete node in binary search tree python

The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst = BSTRee() bst.insert(5) bst.inser…

ImportError: cannot import name cbook when using PyCharms Profiler

I am trying to run the PyCharm profiler but I get the following error message:Traceback (most recent call last):File "/home/b3053674/ProgramFiles/pycharm-2017.1.4/helpers/profiler/run_profiler.py&…

Replicating SAS first and last functionality with Python

I have recently migrated to Python as my primary tool for analysis and I am looking to be able to replicate the first. & last. functionality found in SAS. The SAS code would be as follows;data data…

Can mypy track string literals?

Is there anyway to make this work from typing import Literal def foo(bar: Literal["bar"]) -> Literal["foo"]:foo = "foo"return foobar = "bar" foo(bar)Here are …

Lazy loading of attributes

How would you implement lazy load of object attributes, i.e. if attributes are accessed but dont exist yet, some object method is called which is supposed to load these?My first attempt isdef lazyload…

Using pythons property while loading old objects

I have a rather large project, including a class Foo which recently needed to be updated using the @property decorator to create custom getter and setter methods.I also stored several instances of Foo …

How to replace all those Special Characters with white spaces in python?

How to replace all those special characters with white spaces in python ?I have a list of names of a company . . . Ex:-[myfiles.txt] MY company.INCOld Wine pvtmaster-minds ltd"apex-labs ltd"…