简体   繁体   English

从熊猫数据框中删除行和列

[英]Delete row and column from pandas dataframe

I have a CSV file which is contains a symmetric adjacency matrix which means row and column have equivalent labels. 我有一个CSV文件,其中包含一个对称的邻接矩阵,这意味着行和列具有等效标签。

I would like to import this into a pandas dataframe, ideally have some GUI pop up and ask for a list of items to delete....and then take that list in and set the values in the relative row and column as zero's and return a separate altered dataframe. 我想将其导入到pandas数据框中,理想情况下会弹出一些GUI,并要求删除项目列表...。然后将其放入列表中,并将相对行和列中的值设置为零,然后返回一个单独的更改后的数据框。

In short, something that takes the following matrix 简而言之,它采用以下矩阵

a b c d e

a 0 3 5 3 5 一个0 3 5 3 5

b 3 0 2 4 5 b 3 0 2 4 5

c 5 2 0 1 7 c 5 2 0 1 7

d 3 4 1 0 9 d 3 4 1 0 9

e 5 5 7 9 0 e 5 5 7 9 0

Pops up a simple interface asking "which regions should be deleted" and a line to enter those regions 弹出一个简单的界面,询问“应删除哪些区域”和一行输入这些区域的行

and say c and e are entered 并说输入了c和e

returns 回报

abcde ABCDE

a 0 3 0 3 0 a 0 3 0 3 0

b 3 0 0 4 0 b 3 0 0 4 0

c 0 0 0 0 0 c 0 0 0 0 0

d 3 4 0 0 0 d 3 4 0 0 0

e 0 0 0 0 0 e 0 0 0 0 0

with the altered entries as shown in bold 条目以粗体显示

it should be able to do this for as many areas as entered which can be up to 379....ideally seperated by commas 它应该能够在所输入的尽可能多的区域中执行此操作,最多可以输入379。...最好用逗号分隔

Set columns and rows by index values with DataFrame.loc : 使用DataFrame.loc通过索引值设置列和行:

vals = ['c','e']
df.loc[vals, :] = 0

df[vals] = 0
#alternative
#df.loc[:, vals] = 0
print (df)
   a  b  c  d  e
a  0  3  0  3  0
b  3  0  0  4  0
c  0  0  0  0  0
d  3  4  0  0  0
e  0  0  0  0  0

Another solution is create boolean mask with numpy broadcasting and set values by DataFrame.mask : 另一个解决方案是使用numpy广播创建布尔型掩码,并通过DataFrame.mask设置值:

mask = df.index.isin(vals) | df.columns.isin(vals)[:, None]
df = df.mask(mask, 0)
print (df)
   a  b  c  d  e
a  0  3  0  3  0
b  3  0  0  4  0
c  0  0  0  0  0
d  3  4  0  0  0
e  0  0  0  0  0

Start by importing the csv: 首先导入csv:

import pandas as pd
adj_matrix = pd.read_csv("file/name/to/your.csv", index_col=0)

Then request the input: 然后请求输入:

regions = input("Please enter the regions that you want deleted (as an array of strings)")
adj_matrix.loc[regions, :] = 0
adj_matrix.loc[:, regions] = 0

Now adj_matrix should be in the form you want. 现在adj_matrix应该是您想要的形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM