【问题标题】:Seperate columns into list after applying df.groupby() in pandas在 pandas 中应用 df.groupby() 后将列分隔到列表中
【发布时间】:2018-06-09 22:37:45
【问题描述】:

这是原始数据。

ID &nbsp &nbsp 时间 &nbsp &nbsp 字节

1 &nbsp &nbsp 13:00 &nbsp &nbsp 10

2 &nbsp &nbsp 13:02 &nbsp &nbsp 30

3 &nbsp &nbsp 13:03 &nbsp &nbsp 40

4 &nbsp &nbsp 13:02 &nbsp &nbsp 50

5 &nbsp &nbsp 13:03 &nbsp &nbsp 70


我使用ax = server_logs.groupby('TIME')['REPLY_SIZE'].sum() 获得了以下数据。

ID &nbsp &nbsp 时间 &nbsp &nbsp 字节

1 &nbsp &nbsp 13:00 &nbsp &nbsp 10

2 &nbsp &nbsp 13:02 &nbsp &nbsp 80

3 &nbsp &nbsp 13:03 &nbsp &nbsp 110

做完之后如何将 TIME 和 BYTES 分成两个不同的列表?使用time = ax[0] 似乎没有分开。

ps:我想在此数据上使用 sklearn 应用 k 表示聚类。

【问题讨论】:

  • 怎么样:v = df.groupby('TIME')['BYTES'].sum(); a, b = v.index.tolist(), v.tolist()?
  • 不,它们是时间戳对象。您可以毫无问题地绘制它们。是的,这就是它的作用。另外,使用 pd.Series.plot 有什么问题?就像我在你上一个问题中建议的那样?
  • 是的。我画了。有用。我在上一个问题中找不到您的 pd.Series.plot 。可以发在这里吗?
  • 你本来可以做到的df.groupby('TIME')['BYTES'].sum().plot() :p

标签: python-3.x pandas k-means pandas-groupby sklearn-pandas


【解决方案1】:
time=ax[:,0]
bytes=ax[:,1]

你可以试试吗?

如果这不起作用,那么应该这样做

time=ax["TIME"]
bytes=ax["BYTES"]

【讨论】:

  • 使用time = ax[:,0],我收到以下错误Can only tuple-index with a MultiIndex。使用time=ax["TIME"],我收到此错误:Can only tuple-index with a MultiIndex
【解决方案2】:

@COLDSPEED 给出的答案。

v = df.groupby('TIME')['BYTES'].sum(); 
a, b = v.index.tolist(), v.tolist()

【讨论】:

    猜你喜欢
    • 2013-02-25
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2020-09-26
    • 2017-10-25
    • 2021-03-05
    相关资源
    最近更新 更多