yren
yren

Reputation: 103

Pandas move rows from 1 DF to another DF

I have df1 read from Excel, then I create an empty df2 with the same columns. Now I want to move some rows from df1 matching some condition to df2. Is there any easy way to do this like pop() in list, meaning the item can be popped to new list and deleted from the old list.

What I am doing is append these rows to df2, then df1=df1[~condition] to remove them from df1, but I always got annoying warnings:

"UserWarning: Boolean Series key will be reindexed to match DataFrame index.
"DataFrame index.", UserWarning)"

I think above warning is due to "df1=df1[~condition]", after comment this the warning disappeared.

Upvotes: 10

Views: 28372

Answers (1)

Alexander
Alexander

Reputation: 109546

If you do not care about your index (which it appears you do not), then you can do the following:

np.random.seed(0)
df1 = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
df2 = pd.DataFrame(columns=df1.columns)

>>> df1
          A         B         C
0  1.764052  0.400157  0.978738
1  2.240893  1.867558 -0.977278
2  0.950088 -0.151357 -0.103219
3  0.410599  0.144044  1.454274
4  0.761038  0.121675  0.443863

cond = df1.A < 1
rows = df1.loc[cond, :]
df2 = df2.append(rows, ignore_index=True)
df1.drop(rows.index, inplace=True)

>>> df1
          A         B         C
0  1.764052  0.400157  0.978738
1  2.240893  1.867558 -0.977278

>>> df2
          A         B         C
0  0.950088 -0.151357 -0.103219
1  0.410599  0.144044  1.454274
2  0.761038  0.121675  0.443863

Upvotes: 30

Related Questions