Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pyPandas functionality request: reverse/negative df.drop

Tags:

python

pandas

I think a reverse/negative dataframe.drop functionality would be a very useful tool. Has anybody have a overcome to this?

like image 346
fred Avatar asked Sep 28 '12 18:09

fred


People also ask

How do you remove a specific value from a data frame?

Use drop() method to delete rows based on column value in pandas DataFrame, as part of the data cleansing, you would be required to drop rows from the DataFrame when a column value matches with a static value or on another column value.

What does DF drop do in Python?

Pandas DataFrame drop() Method The drop() method removes the specified row or column. By specifying the column axis ( axis='columns' ), the drop() method removes the specified column. By specifying the row axis ( axis='index' ), the drop() method removes the specified row.

How do you remove a row from pandas DataFrame based on the length of the column values?

To delete rows from a pandas DataFrame based on a conditional expression involving len(string) giving KeyError you can do len(df['column name']) you are just getting one number, namely the number of rows in the DataFrame (i.e., the length of the column itself).

How do I delete rows in pandas DataFrame based on condition?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).


2 Answers

Generally, I find myself using boolean indexing and the tilde operator when obtaining the inverse of a selection, rather than df.drop(), though the same concept applies to df.drop when boolean indexing is used to form the array of labels to drop. Hope that helps.

In [44]: df
Out[44]: 
          A         B
0  0.642010  0.116227
1  0.848426  0.710739
2  0.563803  0.416422

In [45]: cond = (df.A > .6) & (df.B > .3)

In [46]: df[cond]
Out[46]: 
          A         B
1  0.848426  0.710739

In [47]: df[~cond]
Out[47]: 
          A         B
0  0.642010  0.116227
2  0.563803  0.416422
like image 146
Garrett Avatar answered Oct 30 '22 23:10

Garrett


If I understand you right, you can get this effect just by indexing with an "isin" on the index:

>>> df
         A         B         C
0  0.754956 -0.597896  0.245254
1 -0.987808  0.162506 -0.131674
2 -1.064639 -2.193629  1.814078
3 -0.483950 -1.290789  1.776827
4 -0.191055 -0.461204  0.412220
>>> df[df.index.isin([0, 2, 3])]   # Drop rows whose label is not in the set [0, 2, 3]
         A         B         C
0  0.754956 -0.597896  0.245254
2 -1.064639 -2.193629  1.814078
3 -0.483950 -1.290789  1.776827
like image 20
BrenBarn Avatar answered Oct 30 '22 22:10

BrenBarn