Issue
Now that append()
is removed in pandas 2.0, what is a short alternative to append()
allowing method chaining?
The "What’s new in 2.0.0" section of pandas says:
Removed deprecated
Series.append()
,DataFrame.append()
, useconcat()
instead (GH 35407)
I am looking for something like below to add one row, but within a method chain.
import pandas as pd
df = pd.DataFrame([[4, 5, 6],
[7, 8, 9],
])
df.loc[df.index.size] = [10, 20, 30]
Solution
Since you want to use method chaining, use pipe
and concat
:
out = (df
#.some_method()
.pipe(lambda d: pd.concat(
[d, pd.DataFrame([[10, 20, 30]],
columns=df.columns)],
ignore_index=True)
)
#.some_other_method()
)
Or, if adding the row is the first step:
out = (pd.concat([d, pd.DataFrame([[10, 20, 30]],
columns=df.columns)],
ignore_index=True)
#.other_methods()
)
Output:
0 1 2
0 4 5 6
1 7 8 9
2 1 6 4
3 10 20 30
Note that adding a new row is expensive as this creates a full new DataFrame. It's fine if you do it once or twice but don't do it in a loop or this will be very slow for large datasets.
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.