DEV Community

Punitha
Punitha

Posted on

Selecting Rows, Missing Values and Duplicate Values

  • loc[] is used to select rows and columns using labels.
print(df.loc[0])

print(df.loc[0, "Name"])
Enter fullscreen mode Exit fullscreen mode
  • iloc[] selects data using integer positions.
print(df.iloc[0])

print(df.iloc[0, 0])
Enter fullscreen mode Exit fullscreen mode
  • isin() checks whether values belong to a given list.
result = df[df["Name"].isin(["Arun", "Ravi"])]

print(result)
Enter fullscreen mode Exit fullscreen mode
  • between() checks whether values are within a range.
result = df[df["Marks"].between(70, 90)]

print(result)
Enter fullscreen mode Exit fullscreen mode

Renaming Columns

df = df.rename(
    columns={"Marks": "Score"}
)
print(df)
Enter fullscreen mode Exit fullscreen mode

Sorting Data

  • Ascending
df = df.sort_values("Marks")

print(df)
Enter fullscreen mode Exit fullscreen mode
  • Descending
df = df.sort_values("Marks", ascending=False)

print(df)
Enter fullscreen mode Exit fullscreen mode

Missing Values

NaN means the value is missing.

  • Finding Missing Values
print(df.isnull())

print(df.isnull().sum())
Enter fullscreen mode Exit fullscreen mode
  • Removing Missing Values
df = df.dropna()
Enter fullscreen mode Exit fullscreen mode
  • Filling Missing Values
df["Marks"] = df["Marks"].fillna(0)
Enter fullscreen mode Exit fullscreen mode

Duplicate Values

  • Find duplicates
print(df.duplicated())
Enter fullscreen mode Exit fullscreen mode
  • Remove duplicates
df = df.drop_duplicates()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)