How to change dataframe column values importing from csv file?

By Rama, 6 months ago
  • Bookmark
0

Import the boston housing dataset, but while importing change the 'medv' (median house value) column so that values < 35 becomes ‘Low’ and > 35 becomes ‘High’.

Datarame
1 Answer
0

import csv
with open('BostonHousing.csv', 'r') as f:
    reader = csv.reader(f)
    out = []
    for i, row in enumerate(reader):
        if i > 0:
            row[13] = 'High' if float(row[13]) > 35 else 'Low'
        out.append(row)

df = pd.DataFrame(out[1:], columns=out[0])
print(df.head())

Your Answer

Webinars

More webinars

Related Discussions

Running random forest algorithm with one variable

View More
We're Online!

Chat now for any query