Pandas Column Construction with np.where()

pandas

Solution

Just in case, you can create a new column with much less effort. E.g.:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: df = pd.DataFrame(np.random.uniform(size=10))

In [4]: df
Out[4]: 
          0
0  0.366489
1  0.697744
2  0.570066
3  0.756647
4  0.036149
5  0.817588
6  0.884244
7  0.741609
8  0.628303
9  0.642807

In [5]: categorize = lambda value: "ABC"[int(value > 0.3) + int(value > 0.6)]

In [6]: df["new_col"] = df[0].apply(categorize)

In [7]: df
Out[7]: 
          0 new_col
0  0.366489       B
1  0.697744       C
2  0.570066       B
3  0.756647       C
4  0.036149       A
5  0.817588       C
6  0.884244       C
7  0.741609       C
8  0.628303       C
9  0.642807       C

Problem

I'm working through an assignment with Pandas and am using np.where() to create add a column to a Pandas DataFrame with three possible values: ``` fips_df['geog_type'] = np.where(fips_df.fips.str[-3:] != '000', 'county', np.where(fips_df.fips.str[:] == '00000', 'country', 'state')) ``` The state of the DataFrame after adding the column is like this: ``` print fips_df[:5] fips geog_entity fips_prefix geog_type 0 00000 UNITED STATES 00 country 1 01000 ALABAMA 01 state 2 01001 Autauga County, AL 01 county 3 01003 Baldwin County, AL 01 county 4 01005 Barbour County, AL 01 county ``` This column construction is tested by two asserts. The first passes and the second fails. ``` ## check the numbers of geog_type assert set(fips_df['geog_type'].value_counts().iteritems()) == set([('state', 51), ('country', 1), ('county', 3143)]) assert set(fips_df.geog_type.value_counts().iteritems()) == set([('state', 51), ('country', 1), ('county', 3143)]) ``` What is the difference between calling columns as fips_df.geog_type and fips_df['geog_type'] that causes my second assert to fail?

Original source