'Elegant alternative to df[df.columns[0]]

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.



Solution 1:[1]

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

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

Solution 2:[2]

# 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 ...]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ynjxsjmh
Solution 2