【问题标题】:Extracting a matrix from dataframe从数据框中提取矩阵
【发布时间】:2021-06-02 10:09:50
【问题描述】:

我有这样的事情:

import holoviews as hv
import pandas as pd
from holoviews import opts, dim
hv.extension('bokeh')
renderer = hv.renderer('bokeh')

csv_path = r'C:\Users\jose\Downloads\enron-v1.csv'
df_csv = pd.read_csv(csv_path ,index_col=0)

df_filter = df_csv[["fromJobtitle", "toJobtitle"]]
df_final = df_filter.groupby(df_filter.columns.tolist(),as_index=False).size()

这会产生这样的结果:

 fromJobtitle         toJobtitle  size
0              CEO                CEO    65
1              CEO           Director    23
2              CEO           Employee    56
3              CEO    In House Lawyer     7
4              CEO            Manager   104

我想提取这个矩阵作为将数据绘制为 holoviews 中的桑基图的一种方式:

[['CEO', 'CEO', 65],
['CEO', 'Director', 23],
['CEO', 'Employee', 56]]
.......etc

【问题讨论】:

  • 我认为您不需要这样做。 Holoviews 也对数据框感到满意。但是,我认为 sankey 不适合在两列中使用 CEO,这应该会引发错误。

标签: python dataframe matrix sankey-diagram


【解决方案1】:

pd.DataFrame 已经以这种格式存储它们,所以你只需要这样做:

df_final.values

Out[149]: 
array([['CEO', 'CEO', 65],
       ['CEO', 'Director', 23],
       ['CEO', 'Employee', 56],
       ['CEO', 'Lawyer', 7],
       ['CEO', 'Manager', 104]], dtype=object)

【讨论】:

    猜你喜欢
    • 2019-08-08
    • 1970-01-01
    • 2021-10-14
    • 2022-01-04
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多