I only want the column name when iterating using
for index, row in df.iterrows()
Let’s discuss how to get column names in Pandas dataframe. First, let’s create a simple dataframe with nba.csv file. Now let’s try to get the columns name from above dataset. Method #3: column.values method returs an array of index. Method #4: Using tolist () method with values with given the list of columns.
In Pandas Dataframe we can iterate an element in two ways: 1 Iterating over rows 2 Iterating over columns More ...
Using a DataFrame as an example. You can use the iteritems () method to use the column name (column name) and the column data (pandas. Series) tuple (column name, Series) can be obtained. You can use the iterrows () method to use the index name (row name) and the data (pandas. Series) tuple (index, Series) can be obtained.
Now we apply iterrows () function in order to get a each element of rows. In order to iterate over rows, we use iteritems () function this function iterates over each column as key, value pair with the label as key, and column value as a Series object. Now we apply a iteritems () function in order to retrieve an rows of dataframe.
When iterating over a dataframe using df.iterrows
:
for i, row in df.iterrows():
...
Each row row
is converted to a Series, where row.index
corresponds to df.columns
, and row.values
corresponds to df.loc[i].values
, the column values at row i
.
Minimal Code Sample
df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}, index=['a', 'b'])
df
A B
a 1 3
b 2 4
row = None
for i, row in df.iterrows():
print(row['A'], row['B'])
# 1 3
# 2 4
row # outside the loop, `row` holds the last row
A 2
B 4
Name: b, dtype: int64
row.index
# Index(['A', 'B'], dtype='object')
row.index.equals(df.columns)
# True
row.index[0]
# A
You are already getting to column name, so if you just want to drop the series you can just use the throwaway _
variable when starting the loop.
for column_name, _ in df.iteritems():
# do something
However, I don't really understand the use case. You could just iterate over the column names directly:
for column in df.columns:
# do something
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With