【问题标题】:Transform Matrix - Showing all the 0 value变换矩阵 - 显示所有 0 值
【发布时间】:2019-11-01 20:38:15
【问题描述】:

我有一个巨大的数据框。数据如下所示:

Person  Distance    BS
A       125.58      BS3
A       212.01      BS4
B       11.41       BS3
B       134.35      BS2
C       11.41       BS3
C       274.20      BS2
D       220.98      BS5
D       8.01        BS7
E       606.05      BS1
E       676.88      BS2
F       28.81       BS7
F       98.69       BS5
G       81.64       BS1
G       35.49       BS3

我根据这个问题 Is it possible from dataframe transform to Matrix? 使用以下代码将此数据集转换为 OD 矩阵:

df = pd.read_csv("data.csv")
df = df[df.Distance < 100]
df = df[df.groupby('Person').Person.transform(len) > 1]
places = df["BS"].unique()
places.sort()
od_df = pd.DataFrame(df["BS"].values.reshape((-1, 2)), columns=["O", "D"])
od_matrix = pd.pivot_table(od_df, index="O", columns="D", aggfunc="size").reindex(index=places, columns=places)
od_matrix.fillna(0, downcast="infer", inplace=True)
od_matrix

我想消除100米以上的距离。因此,我输入了distance &lt; 100。结果如下所示:

D   BS1 BS3 BS5 BS7
O               
BS1 0   1   0   0
BS3 0   0   0   0
BS5 0   0   0   0
BS7 0   0   1   0

如果我的大数据是从 BS1 到 BS9,在消除矩阵列和行之后,没有数据 (0) 也将消失。如果没有数据(0),如何显示所有列和行事件?。我想展示的矩阵是这样的:

D   BS1 BS2 BS3 BS4 BS5 BS6 BS7 BS8 BS9
O                                   
BS1 0   0   1   0   0   0   0   0   0
BS2 0   0   0   0   0   0   0   0   0
BS3 0   0   0   0   0   0   0   0   0
BS4 0   0   0   0   0   0   0   0   0
BS5 0   0   0   0   0   0   0   0   0
BS6 0   0   0   0   0   0   0   0   0
BS7 0   0   0   0   1   0   0   0   0
BS8 0   0   0   0   0   0   0   0   0
BS9 0   0   0   0   0   0   0   0   0

【问题讨论】:

    标签: python pandas matrix row multiple-columns


    【解决方案1】:

    首先是重新分配DataFrame 到df1,通过使用 f 字符串的列表理解获得唯一的places,并将fill_value=0 参数添加到函数pivot_table 和reindex:

    df1 = df[df.Distance < 100]
    df1 = df1[df1.groupby('Person').Person.transform(len) > 1]
    places = [f'BS{ x + 1}' for x in range(9)]
    print (places)
    ['BS1', 'BS2', 'BS3', 'BS4', 'BS5', 'BS6', 'BS7', 'BS8', 'BS9']
    
    
    od_df = pd.DataFrame(df1["BS"].values.reshape((-1, 2)), columns=["O", "D"])
    od_matrix = (pd.pivot_table(od_df, index="O", columns="D", aggfunc="size", fill_value=0)
                   .reindex(index=places, columns=places, fill_value=0))
    

    或者:

    od_matrix = (pd.crosstab(od_df["O"], od_df["D"])
                   .reindex(index=places, columns=places, fill_value=0))
    

    print (od_matrix)
    D    BS1  BS2  BS3  BS4  BS5  BS6  BS7  BS8  BS9
    O                                               
    BS1    0    0    1    0    0    0    0    0    0
    BS2    0    0    0    0    0    0    0    0    0
    BS3    0    0    0    0    0    0    0    0    0
    BS4    0    0    0    0    0    0    0    0    0
    BS5    0    0    0    0    0    0    0    0    0
    BS6    0    0    0    0    0    0    0    0    0
    BS7    0    0    0    0    1    0    0    0    0
    BS8    0    0    0    0    0    0    0    0    0
    BS9    0    0    0    0    0    0    0    0    0
    

    【讨论】:

    • 感谢您的回答。即使值为0,如何完成BS(BS1到BS9)?
    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 2012-04-24
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多