How to Remove All Rows from a Data.Frame

How to delete all rows in a dataframe?

The latter is possible and strongly recommended - "inserting" rows row-by-row is highly inefficient. A sketch could be

>>> import numpy as np
>>> import pandas as pd
>>> index = np.arange(0, 10)
>>> df = pd.DataFrame(index=index, columns=['foo', 'bar'])
>>> df
Out[268]:
foo bar
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
5 NaN NaN
6 NaN NaN
7 NaN NaN
8 NaN NaN
9 NaN NaN

Remove all rows having a specific value in dataframe

you check df1 != 999.9 in this check only first row delete because for other row you have column that != 999.9.

Try this:

>>> mask = (df1 == 999.9).any(1)
>>> df1[~mask]

# for more explanation

>>> df1 == 999.9
a b
0 True True
1 True False
2 False True
3 False False
4 False False

in your solution:

>>> (df1 != 999.9)
a b
0 False False
1 False True
2 True False
3 True True
4 True True

>>> (df1 != 999.9).any(axis = 1) # for check rows
0 False
1 True
2 True
3 True
4 True
dtype: bool

>>> (df1 != 999.9).any(axis = 0) # for check columns
a True
b True
dtype: bool

Deleting DataFrame row in Pandas based on column value

If I'm understanding correctly, it should be as simple as:

df = df[df.line_race != 0]

How to remove rows in a Pandas dataframe if the same row exists in another dataframe?

You an use merge with parameter indicator and outer join, query for filtering and then remove helper column with drop:

DataFrames are joined on all columns, so on parameter can be omit.

print (pd.merge(a,b, indicator=True, how='outer')
.query('_merge=="left_only"')
.drop('_merge', axis=1))
0 1
0 1 10
2 3 30

Pandas: How to remove rows from a dataframe based on a list?

You need

new_df = sales[~sales.CustomerID.isin(badcu)]

How to remove all rows from a data.frame?

If you really want to delete all rows:

> ddf <- ddf[0,]
> ddf
[1] vint1 vint2 vfac1 vfac2
<0 rows> (or 0-length row.names)

If you mean by keeping the structure using placeholders:

> ddf[,]=matrix(ncol=ncol(ddf), rep(NA, prod(dim(ddf))))
> ddf
vint1 vint2 vfac1 vfac2
1 NA NA NA NA
2 NA NA NA NA
3 NA NA NA NA
4 NA NA NA NA
5 NA NA NA NA


Related Topics



Leave a reply



Submit