0

Is there a way to slice columns in data frame when I want multiple consecutive columns? I also want to be able to slice the rows at the same time. The below example shows the call I would like to find an equivalent to.

import numpy as np
import pandas as pd


data = np.random.randn(10, 4)
df = pd.DataFrame(data, columns=['A', 'B', 'C', 'D'])

print(df.loc[1:5, ['A', 'B', 'C']])  # Works
# print(df.loc[1:5, ['A':'C']])  # Is there a syntax that allows this?
ryanh153
  • 195
  • 6

1 Answers1

0

The correct call is

print(df.loc[1:5, 'A':'C'])

Thanks to @Henry_Yik for the answer.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
ryanh153
  • 195
  • 6