In a table like the one below, I would like to get the row and column numbers (or row, column names) with element 1 in tabular form. At this time, is there a way to do it without using the for statement?
df = pd.DataFrame ([[0,0], [0,1], [1,0]], columns = ['A','B'], index = ['a','b',' c']) How to use # for statement (I really want to do it without using for statement) result =  for i in range (len (df.index)): for j in range (len (df.columns)): if df.iloc [i, j] == 1: result.append ((i, j)) print (result) # [(1, 1), (2, 0)]
Answer # 1
It seems that pandas alone can not be done without using for, but it seems that it can be done by using numpy's nonzero function.
import pandas as pd df = pd.DataFrame ([[0,0], [0,1], [1,0]], columns = ['A','B'], index = ['a','b',' c']) arr = (df == 1) .to_numpy () result = list (zip (* arr.nonzero ())) print (result) # [(1, 1), (2, 0)]
The nonzero function returns the index of a non-zero (not False this time) element.
What is returned is the tuple shape (0-dimensional index array, 1-dimensional index array) (in this example ([1, 2], [1, 0])).
So I use zip to transform it into an array of tuples (0D index, 1D index).
- python - i want to extract only specific rows in a two-dimensional array
- python - i want to modify the value of a specific column or row of pandas using the apply function and the lambda function
- python - updating a specific column in each row does not work in the case of duplicate index in dataframe
- python 3x - beautiful soup i want to extract a specific character string
- i want to extract specific information from a python string
- python: i want to extract only specific lines in array
- how to specify a column from one csv file in python and read it as list type or dict type
- python amicable numbers
- ruby - i want to retrieve only the values of a specific column from the rails array
- substitute the value of a specific line of csv read into python
- i want to extract one character string of text that exists in multiple files with python
- python - how to get the number of searches for a specific word within the period
- python - i want to exclude numbers from csv created with pandas
- python - sum of numbers by inputting a character string
- python 3x - python i want to add the last weekday of the month as a new column to the data frame
- vba - i want to count the numbers in the column for each item and insert a blank row according to the number
- i want to aggregate a specific character string by day using the python groupby count function etc
- i want to extract only those that have common items in the dataset [python]
- if the integer in the table created by dataframe and the ejected integer are the same, i want to make a conditional branch
- python - i don't understand the indexing law of pandasseries i don't know how to take it out
- python 3x - replace does not change
- numpy - [python] i want to combine alternately and make two sets of two arrays of latitude and longitude
- python - i want to create a new dataframe by repeating the work of extracting the average value of the dataframe
- macos (osx) - python: exporting the list to txt gives "" double quotes
- python 3x - i want to concatenate data using pandas merge, but it doesn't work
- csv - unable to delete row in pandas
- python 3x - i want to save the csv file saved by jupyter lab on the desktop