Home > Back-end >  Elegant alternative to df[df.columns[0]]
Elegant alternative to df[df.columns[0]]

Time:04-20

If I want to call the first column of a df without knowing the name of the df, I usually use df[df.columns[0]]. When I use boolean indexing it looks for example like this:

df[df[df.columns[0] > value].count()

Is there a more elegant way to write this? This nesting appears to be very error-prone.

CodePudding user response:

You can use pandas.DataFrame.iloc to index dataframe based on integer position.

df.iloc[:, 0].gt(value).sum()

CodePudding user response:

importing pandas package

import pandas as pd

making data frame from csv file

data = pd.read_csv("file.csv")

df = pd.DataFrame(data, index = [0, 1, 2, 3, 4, 5, ....])

df[[True, False, True, False, True, False ...]]

  • Related