Data Science Explorer

Pandas: Fixing Wrong Data 본문

Python

Pandas: Fixing Wrong Data

grace21110 2023. 10. 29. 16:09
반응형

Wrong data is just a wrong data like if someone registered 90 instead of 9.0. 

 

  • Replacing Values 

One way to fix wrong values is to replace them with something else. 

 

Example 

Set "Duration" = 80 in row 2. 

df.loc[2, 'Duration'] = 80

 

  • Removing Rows

Another way of handling wrong data is to remove the rows that contains wrong data. 

Example 

Delete rows where "Duration" is higher than 130.

for x in df.index: 
	if df.loc[x, "Duration"] > 130: 
    df.drop(x, inplace = True)

 

Exercise

Loop through all values in the "Duration" column. If the value is higher than 120, set it to 120.

for x in df.index: 
	if df.loc[x, "Duration"] > 120: 
   	df.loc [x, "Duration"] = 120

 

'Python' 카테고리의 다른 글

Matplotlib  (0) 2023.11.04
Pandas: Removing Duplicates  (0) 2023.10.30
Pandas: Cleaning Data of Wrong Format  (0) 2023.10.29
Pandas: Cleaning Empty Cells  (2) 2023.10.29
Pandas Read CSV  (0) 2023.10.28