Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the column name when iterating through dataframe pandas?

Tags:

python

pandas

I only want the column name when iterating using

for index, row in df.iterrows()
like image 792
veridian Avatar asked Nov 25 '17 01:11

veridian


People also ask

How to get column names in pandas Dataframe?

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.

How to iterate an element in a pandas Dataframe?

In Pandas Dataframe we can iterate an element in two ways: 1 Iterating over rows 2 Iterating over columns More ...

How to use a Dataframe as an example?

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.

How to iterate over rows in a Dataframe in Python?

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.


2 Answers

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
like image 131
cs95 Avatar answered Sep 28 '22 16:09

cs95


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
like image 39
Sraffa Avatar answered Sep 28 '22 17:09

Sraffa