从数据框中删除任何值

从数据框中删除任何值

本文介绍了从数据框中删除任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,如:

    Country Name    Income Group
 1  Norway          High income
 2  Switzerland     Middle income
 3  Qatar           Low income
 4  Luxembourg      Low income
 5  Macao           High income
 6  India           Middle income

我需要类似的东西:

    High income     Middle income   Low income
1   Norway           Switzerland     Qatar
2    Macao              India         Luxembourg

我已经使用数据透视表:df = df.pivot(值=国家/地区名称",索引=无,列=收入组")我得到类似的东西:

I have used pivot tables :df= df.pivot(values='Country Name', index=None, columns='Income Group')and i get something like :

    High income     Middle income   Low income
1   Norway           none            none
2    none           Switzerland      none
 .
 .
 .

有人可以提出比在这里枢纽解决方案更好的解决方案,这样我就不必处理任何值了吗?

Can someone suggest a better solution than pivot here so that i don't have to deal with none values?

推荐答案

技巧是引入一个新列index,其值是groupby/cumcount值. cumcount返回一个累积计数-从而对每个组中的项目进行编号:

The trick is to introduce a new column index whose values are groupby/cumcount values. cumcount returns a cumulative count -- thus numbering the items in each group:

df['index'] = df.groupby('Income Group').cumcount()
#   Country Name   Income Group  index
# 1       Norway    High income      0
# 2  Switzerland  Middle income      0
# 3        Qatar     Low income      0
# 4   Luxembourg     Low income      1
# 5        Macao    High income      1
# 6        India  Middle income      1

一旦有了index列,就可以通过以下操作获得所需的结果:

Once you have the index column, the desired result can be obtained by pivoting:

import pandas as pd
df = pd.DataFrame({'Country Name': ['Norway', 'Switzerland', 'Qatar', 'Luxembourg', 'Macao', 'India'], 'Income Group': ['High income', 'Middle income', 'Low income', 'Low income', 'High income', 'Middle income']})

df['index'] = df.groupby('Income Group').cumcount() + 1
result = df.pivot(index='index', columns='Income Group', values='Country Name')
result.index.name = result.columns.name = None
print(result)

收益

  High income  Low income Middle income
1      Norway       Qatar   Switzerland
2       Macao  Luxembourg         India

这篇关于从数据框中删除任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:21