简体   繁体   中英

How to extract concrete data from a column of a dataframe

I want to extract data from one column based on another, an example will be clearer.

in one column I have direction data (0º - 360º), and in another I have wind speed data, so for example I want to know the average wind speed in a northerly direction (360º) for this I want for all 360º values in the direction column extract the corresponding data in the speed column. so to be able to calculate the average, as the sum of the speed / frequency

I know that with the df.drop function I can put conditions and eliminate the rows that meet it, would there be a function that allows me to extract those rows

If someone comes up with a method to extract this data according to each address, I would appreciate it.

What you need is the groupby method.

Assuming that:

  • your dataframe is named df
  • your direction column is called direction
  • your wind speed column is called wind_speed ...

...you can simply run:

df.groupby('direction').mean()['wind_speed']

...to compute the average wind speed for each direction in the direction column.

Try this

import pandas as pd

#sample DF
df = pd.DataFrame({'direction': [0, 45, 90, 135, 180, 225, 270, 315, 360],
                   'speed': [5, 10, 15, 20, 25, 30, 35, 40, 45]})

# extract rows where direction is 360
df_360 = df.loc[df['direction'] == 360, ['speed']]

# calculate the mean wind speed in a northerly direction
mean_speed = df_360['speed'].mean()

print(mean_speed)  # prints 45.0

is it helpful?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM