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.
shift() If you want to shift your column or subtract the column value with the previous row value from the DataFrame, you can do it by using the shift() function. It consists of a scalar parameter called period, which is responsible for showing the number of shifts to be made over the desired axis.
append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value.
DataFrame - transpose() function The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa.
iterrows() is used to iterate over a pandas Data frame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in form of series.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With