0

I have a pandas dataframe based on airports that has a type column with values: small_airport, medium_airport, large_airport

I need to extract these values in to 3 different columns and I'm a bit stuck. I have tried various permutations of:

data['small_airport'] = data[['type'] == 'small_airport']

But I can't get anything to work. I have seen several similar posts on here but have not really understood the answers fully enough to implement the suggestions so sorry if I am duplicating questions.

Any advice would be much appreciated! Edit: A sample of the data is:

id, type, name
652, small_airport, Total Rf
345, heliport, Aero B
654, large_airport, Lowell field

It is a university project and the brief states: The ‘type’ column contains information of the type of airport. Extract this out into a new column, one for each category of airport

I hope that helps!

frogger
  • 31
  • 4

2 Answers2

0

You can either use data.groupby('type') and then do aggregations (see here) (which will be most useful for most purposes), or use data.pivot(index=..., columns='type', values=...) (see here) (which is a more exact answer to your question).

Lewistrick
  • 2,649
  • 6
  • 31
  • 42
0

Use pd.crosstab:

out = pd.concat([df, pd.crosstab(df.index, df['type'])], axis=1)
print(out)

# Output
  name            type  large_airport  medium_airport  small_airport
0    A   small_airport              0               0              1
1    B   small_airport              0               0              1
2    C   large_airport              1               0              0
3    D  medium_airport              0               1              0

Note: if you prefer boolean values, use pd.crosstab(...).astype(bool).

Setup:

data = {
    'name': ['A', 'B', 'C', 'D'],
    'type': ['small_airport', 'small_airport', 'large_airport', 'medium_airport']
}
df = pd.DataFrame(df)
Corralien
  • 109,409
  • 8
  • 28
  • 52