Pandas: Creating new data frame from only certain columns

You can make a smaller DataFrame like below:

csv2 = csv1[['Acceleration', 'Pressure']].copy()

Then you can handle csv2, which only has the columns you want. (You said you have an idea about avg calculation.)
FYI, .copy() could be omitted if you are sure about view versus copy.


csv2 = csv1.loc[:, ['Acceleration', 'Pressure']]
  • .loc[] helps keep the subsetting operation explicit and consistent.

  • .loc[] always returns a copy so the original dataframe is never modified.

(for further discussion and great examples of the different view vs. copy alternatives please see: Pandas: Knowing when an operation affects the original dataframe)

Tags:

Python

Pandas

Csv