【问题标题】:How to remove columns in group by Pandas如何通过 Pandas 删除组中的列
【发布时间】:2019-05-01 20:31:42
【问题描述】:

试图删除不再需要的数据列。

我已尝试使用 .drop,但它没有做任何我能说的事情。

df=df.groupby(df['Distributor'])['Tickets Sold'].sum()
df1=df[df.div(df.sum()).lt(0.01)]
df2=df.drop(df1.index)
yourdf=pd.concat([df2,pd.Series(df1.sum(),index=['Others'])])

yourdf = yourdf.sort_values(ascending=False)
print(yourdf)
yourdf2 = yourdf.drop(columns=['Tickets Sold'])
print(yourdf2)

而不是这个。

20th Century Fox      141367982
Focus Features         18799261
Lionsgate              75834308
Paramount Pictures     86302817
STX Entertainment      22606674
Sony Pictures         102746480
Universal             159556790
Walt Disney           315655340
Warner Bros.          216426845
Others                 74618013

然后进入这个。

Walt Disney           315655340
Warner Bros.          216426845
Universal             159556790
20th Century Fox      141367982
Sony Pictures         102746480
Paramount Pictures     86302817
Lionsgate              75834308
Others                 74618013
STX Entertainment      22606674
Focus Features         18799261

我需要这个。

Walt Disney          
Warner Bros.         
Universal             
20th Century Fox      
Sony Pictures         
Paramount Pictures     
Lionsgate              
Others                 
STX Entertainment      
Focus Features

【问题讨论】:

  • 你的数据框只有两列吗?
  • 数据框一共6列680行。
  • @BenPap 我将如何在上面的代码中使用它?

标签: python pandas csv data-analysis


【解决方案1】:

尝试指定axis=1 告诉它您要删除列而不是索引。

yourdf.drop('Tickets Sold', axis=1, inplace=True)
print(yourdf)
#           Distributor
# 0    20th Century Fox
# 1      Focus Features
# 2           Lionsgate
# 3  Paramount Pictures
# 4   STX Entertainment
# 5       Sony Pictures
# 6           Universal
# 7         Walt Disney
# 8         Warner Bros
# 9              Others

如果你真的想保留yourdf 并拥有另一个yourdf2 那么

yourdf2 = yourdf.drop('Tickets Sold', axis=1)

【讨论】:

  • 我怎样才能从 yourdf 中做到这一点,而不是 df 本身,并使其保持从最大到最小的排序。
  • 它对任何包含该列的数据框都一样
  • 当我这样做时,我得到 No axis named 1 for object type
  • 您能否更新您的帖子,以便更清楚地了解哪些打印语句产生了哪些输出?我不确定你是如何应用上面的代码的。
  • 有 144 个发行商和 680 部电影,代码只取发行商以及从该发行商制作的每部电影的售票总额,然后只发布超过 1 的发行商占总门票销售额的百分比,并将所有其他人添加到其他人中。
【解决方案2】:

查看您的 cmets 并阅读您的代码,我认为幕后问题是您过多地转换/重铸变量。这会导致在您需要时覆盖/丢失您正在寻找的内容。不用担心,我确定您正处于项目的第一阶段并进行测试。但我想指出这一点,以防万一。您始终可以使用 inplace=True 关键字参数来解决此问题。

无论如何,正如 Brian Cohan 所说,您需要使用 axis=1 删除轴。

获取您的代码,它看起来像这样。

df = pd.DataFrame(df.groupby(df['Distributor'])['Tickets Sold'].sum()); display(df)
df = df.sort_values(by="Tickets Sold", ascending=False); display(df)
df = df.drop("Tickets Sold", axis = 1); display(df)
# See here ------------------^
|--------------------+--------------|
|                    | Tickets Sold |
|--------------------+--------------|
| Distributor        |              |
|--------------------+--------------|
| 20th Century Fox   |    141367982 |
| Focus Features     |     18799261 |
| Lionsgate          |     75834308 |
| Paramount Pictures |     86302817 |
| STX Entertainment  |     22606674 |
| Sony Pictures      |    102746480 |
| Universal          |    159556790 |
| Walt Disney        |    315655340 |
| Warner Bros.       |    216426845 |
| Others             |     74618013 |
|--------------------+--------------|

|--------------------+--------------|
|                    | Tickets Sold |
|--------------------+--------------|
| Distributor        |              |
|--------------------+--------------|
| Walt Disney        |    315655340 |
| Warner Bros.       |    216426845 |
| Universal          |    159556790 |
| 20th Century Fox   |    141367982 |
| Sony Pictures      |    102746480 |
| Paramount Pictures |     86302817 |
| Lionsgate          |     75834308 |
| Others             |     74618013 |
| STX Entertainment  |     22606674 |
| Focus Features     |     18799261 |
|--------------------+--------------|

|--------------------+
|                    |
|--------------------+
| Distributor        |
|--------------------+
| Walt Disney        |
| Warner Bros.       |
| Universal          |
| 20th Century Fox   |
| Sony Pictures      |
| Paramount Pictures |
| Lionsgate          |
| Others             |
| STX Entertainment  |
| Focus Features     |
|--------------------|

【讨论】:

  • df=df.groupby(df['Distributor'])['Tickets Sold'].sum() df1=df[df.div(df.sum()).lt(0.01) ] df2=df.drop(df1.index) yourdf=pd.concat([df2,pd.Series(df1.sum(),index=['Others'])]) yourdf = yourdf.sort_values(ascending=False) print(yourdf) yourdf2 = yourdf.drop('Tickets Sold', axis=1) print(yourdf2)
  • 我怎样才能实现你在我拥有的代码中所做的,或者如果我用你的代码替换所有这些代码它会起作用?
  • 因为我使用 yourdf 和 yourdf2 来保存原始 df 用于其他用途,而您的 df 和 yourdf2 作为解决其他问题的方法
  • 给我 5 分钟的时间来运行一些测试,看看我能为你做些什么。马上回来。
  • 好的,如果你想用一个 df 来解决各种问题,我会在问题出现时重新编写 df。 df1、df1、df3 等。如果您尝试在其他地方使用这些值,您可能会遇到问题,因为它们是索引值。因此,您需要使用 df.index() 将它们拉出到某种数组中。您可以替换我在您的代码中所做的,但这并不意味着如果我们没有回答/提出正确的问题,它会起作用,我认为这是这里的问题。
猜你喜欢
  • 2019-03-15
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
相关资源
最近更新 更多