I have a question about using iloc for a DataFrame in Python.
iloc recognizes that it can be used to extract data by row and column number.
But why can we extract data other than Age elements with the following code?
csv_titanic.iloc [:, csv_titanic.columns! = "Age"]
Also, does the code that extracts the "Age" element give an error as shown below?
csv_titanic.iloc [:, csv_titanic.columns = "Age"]
Thanks for your response.
Answer # 1
You can select rows and columns using a Boolean array mask. It is a function that is also in numpy (should that be the original)?
Please refer to the document of the link below for a detailed explanation. Or if you search for "pandas boolean selection", an explanation article will appear.
Another common operation is the use of boolean vectors to filter the data.
Indexing and selecting data — pandas 1.0.3 documentation
csv_titanic.columns = "Age"
Python's equality comparison operator is
=Cannot be used in comparisons.
Answer # 2
The explanation of why the data can be extracted or not in the question code is as answered by hayataka2049.
So my answer is supplementary.
get_loc ()There is a method to get the corresponding Index value from the Index name (Column name), so there is also a method to use this when specifying a column with iloc as follows.
csv_titanic.iloc [:, csv_titanic.columns.get_loc ("Age")]
When filtering data, if you specify a column with a Boolean array, the result isDataFrameAlthough it is obtained by type, in this case, it will be specified by numerical value ('Index value'), so the result isSeriesType.
import pandas as pd import numpy as np # Generate dummy data df = pd.DataFrame (np.arange (9) .reshape ((3,3)), columns = ['A', 'B', 'C']) # A B C # 0 0 1 2 # 1 3 4 5 # 2 6 7 8 # Boolean array is obtained by comparison operation as below print (df.columns == 'B') # [False True False] # When passing an array of Boolean values as the specified value of the column # (Result is obtained in DataFrame) print (df.iloc [:, df.columns == 'B']) # B # 0 1 # 14 # 2 7 # How to get Index value corresponding to column name print (df.columns.get_loc ('B')) # 1 # Filter by passing index value # (The result is obtained in Series) print (df.iloc [:, df.columns.get_loc ('B')]) # 0 1 # 14 # 2 7 # Name: B, dtype: int64
- how to specify a column from one csv file in python and read it as list type or dict type
- python 3x - i want to connect the same data of "dataframe" index to one column
- i want to specify a csv column and read it with vbscript
- python - i don't understand the indexing law of pandasseries i don't know how to take it out
- python 3x - i don't know how to specify python version with docker
- python - unable to sort by dropdown selection column in jquery datatables
- python - apply format other than x column in pandas
- i was given a python 3 assignment as a cram school assignment, but i don't understand "functions and comprehensions, while
- python - updating a specific column in each row does not work in the case of duplicate index in dataframe
- python 3x - how to extract row and column numbers with specific values in a dataframe table
- [python] i don't understand why the code can't be executed
- python 3x - python i want to add the last weekday of the month as a new column to the data frame
- python 3x - how to specify multiple conditions with a regular expression
- python dataframe how to save csv divided by date
- python - how to specify the license when using the google custom search api
- specify the save destination of python csv
- python 3x - i want to get a matrix with the values of a certain column removed for each row of the matrix with pytorch (speedu
- in python, adding to the csv file does not work cannot specify field names correctly
- python - pyrhon save colored dataframe color turns black
- linking data with python dataframe
- if the integer in the table created by dataframe and the ejected integer are the same, i want to make a conditional branch
- python 3x - why the element extracted from the list is dtaframe
- python 3x - dataframe column labels are unchanged
- python 3x - pandas: about line feed characters when outputting txt
- for - how to create a table in python that shows all the experimental combinations
- python - about the process of extracting the canceled data based on the absolute value in the data frame
- python 3x - how to extract data that meets the value conditions from another data frame based on the value of one data frame
- python 3x - i get lost when extracting columns in a dataframe
- python 3x - pandas matrix summary table insert columns
- python 3x - pandas: i want to normalize the result aggregated by pivot_table