为了账号安全,请及时绑定邮箱和手机立即绑定

如何去掉最后一列中的零

如何去掉最后一列中的零

偶然的你 2023-07-27 14:13:36
我正在做应用数据科学的作业。问题: 将可再生能源百分比削减为 5 个类别。按大陆划分的前 15 名组,以及这些新的可再生百分比垃圾箱。每个组中有多少个国家?此函数应返回一个具有 Continent MultiIndex 的系列,然后是可再生百分比的 bin。请勿包含没有国家/地区的组。这是我的代码:def answer_twelve():    Top15 = answer_one()    ContinentDict  = {'China':'Asia',                   'United States':'North America',                   'Japan':'Asia',                   'United Kingdom':'Europe',                   'Russian Federation':'Europe',                   'Canada':'North America',                   'Germany':'Europe',                   'India':'Asia',                  'France':'Europe',                   'South Korea':'Asia',                   'Italy':'Europe',                   'Spain':'Europe',                   'Iran':'Asia',                  'Australia':'Australia',                   'Brazil':'South America'}    Top15['Continent'] = Top15.index.to_series().map(ContinentDict)    Top15['bins'] = pd.cut(Top15['% Renewable'],5)    return pd.Series(Top15.groupby(by = ['Continent', 'bins']).size())#,apply(lambda x:s if x['Rank']==0 continue))answer_twelve()这是我对上述代码的输出Continent      bins            Asia           (2.212, 15.753]     4               (15.753, 29.227]    1               (29.227, 42.701]    0               (42.701, 56.174]    0               (56.174, 69.648]    0Australia      (2.212, 15.753]     1               (15.753, 29.227]    0               (29.227, 42.701]    0               (42.701, 56.174]    0               (56.174, 69.648]    0Europe         (2.212, 15.753]     1               (15.753, 29.227]    3               (29.227, 42.701]    2               (42.701, 56.174]    0               (56.174, 69.648]    0North America  (2.212, 15.753]     1               (15.753, 29.227]    0               (29.227, 42.701]    0               (42.701, 56.174]    0               (56.174, 69.648]    1South America  (2.212, 15.753]     0               (15.753, 29.227]    0               (29.227, 42.701]    0               (42.701, 56.174]    0               (56.174, 69.648]    1dtype: int64
查看完整描述

3 回答

?
侃侃无极

TA贡献2051条经验 获得超10个赞

使用 pandas 并在列为零时删除行

如果 column_name 是您的列:

df = df[df.column_name != 0]


查看完整回答
反对 回复 2023-07-27
?
ITMISS

TA贡献1871条经验 获得超8个赞

lambda x:s if x['Rank']==0 continue

这没有任何意义,因为continue仅在循环内有用。请注意,您需要打印一个值。相反,将其留空:

lambda x:"" if x['Rank']==0 else s


查看完整回答
反对 回复 2023-07-27
?
凤凰求蛊

TA贡献1825条经验 获得超4个赞

您可以使用“for”循环迭代这些值,然后使用replace()NaN 替换 0,现在您可以使用dropna(). 我尝试使用drop()或droplevel()代替替换它们,但它不起作用。这是我的代码:


for k,i in series_df.items():

    if i == 0:

        pd_series.replace(to_replace=i, value=np.nan, inplace=True)

        pd_series.dropna(axis=0, inplace=True)

print(pd_series)

您可能需要更改结果的数据类型。输出为:


Continent      bins            

Asia           (2.212, 15.753]     4

               (15.753, 29.227]    1

Australia      (2.212, 15.753]     1

Europe         (2.212, 15.753]     1

               (15.753, 29.227]    3

               (29.227, 42.701]    2

North America  (2.212, 15.753]     1

               (56.174, 69.648]    1

South America  (56.174, 69.648]    1

dtype: int64


查看完整回答
反对 回复 2023-07-27
  • 3 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信