'How to drop row in polars-python

How to add new feature like length of data frame & Drop rows value using indexing. I want to a add a new column where I can count the no-of rows available in a data frame, & using indexing drop rows value.



Solution 1:[1]

Polars doesn't allow much mutation and favors pure data handling. Meaning that you create a new DataFrame instead of modifying an existing one.

So it helps to think of the data you want to keep instead of the row you want to remove.

Below I have written an example that keeps all data except for the 2nd row. Note that the slice will be the fastest of the two and will have zero data copy.

df = pl.DataFrame({
    "a": [1, 2, 3],
    "b": [True, False, None]
}).with_row_count("row_nr")

print(df)

# filter on condition
df_a = df.filter(pl.col("row_nr") != 1)

# stack two slices
df_b = df[:1].vstack(df[2:])

assert df_a.frame_equal(df_b)

print(df_a)

Outputs:

shape: (3, 3)
????????????????????????
? row_nr ? a   ? b     ?
? ---    ? --- ? ---   ?
? u32    ? i64 ? bool  ?
????????????????????????
? 0      ? 1   ? true  ?
????????????????????????
? 1      ? 2   ? false ?
????????????????????????
? 2      ? 3   ? null  ?
????????????????????????
shape: (2, 3)
???????????????????????
? row_nr ? a   ? b    ?
? ---    ? --- ? ---  ?
? u32    ? i64 ? bool ?
???????????????????????
? 0      ? 1   ? true ?
???????????????????????
? 2      ? 3   ? null ?
???????????????????????

Solution 2:[2]

This is available in the docs of pandas data frame. I suggest you check the documentation before coming up with any doubts. This will make you more independent. However, here are the functions/keywords you were looking for:

df.size #Gives the size of the dataframe (Basically multiplying the rows and columns)
df.ndim #Gives the Dimension of the dataframe (2D, 3D, ...)
df.shape #Gives the rows and column count of the dataframe
df.drop([0,1]) #Drops the 0 and 1 index rows in the dataframe
df[column_name] = column_value #Simply creates a new column with the desired values and name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ritchie46
Solution 2 The Myth