pandas实例-Deleting(二)

继续前面的练习,之前的文章参考:


先看数据集

url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data'
df = pd.read_csv(url)

1. Delete the first, fourth, seventh, nineth, eleventh, thirteenth and fourteenth columns

删除某些列

drops = pd.Series([1,4,7,9,11,13,14])
df.columns[drops-1]
df.drop(columns=df.columns[drops-1] , inplace=True)

2. Assign the columns as below:

这一题,就是指定列名,直接来吧,但是有个问题,就是当前的column就没了,这里占位的其实是数据

df.columns=['alcohol', 'malic_acid', 'alcalinity_of_ash', 'magnesium', 'flavanoids', 'proanthocyanins', 'hue']

3. Set the values of the first 3 rows from alcohol as NaN

##  Set the values of the first 3 rows from alcohol as NaN
df.iloc[:3] = np.nan

4. Now set the value of the rows 3 and 4 of magnesium as NaN

# Now set the value of the rows 3 and 4 of magnesium as NaN
df.loc[3:5 , 'magnesium'] = np.nan

哦,上一题我做错了,哈哈哈,我把所有列都设置为nan了

5. Fill the value of NaN with the number 10 in alcohol and 100 in magnesium

df['alcohol'].fillna(10 , inplace=True)
df['magnesium'].fillna(100 , inplace=True)

6. Count the number of missing values

df.isna().sum()

我这里是有缺失值的,但是原文没有,注意下

后面还有几题,我看比较类似,就算了,到这吧,哈

https://www.jianshu.com/p/943e30dab6f7

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
pandas
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论