adding new column to pandas dataframe with values for particular items?
pandas, python
Solution
In [11]: df = pd.DataFrame([{"a": 1}, {"a": 3, "b": 2}])
In [12]: df['c'] = np.array(['foo',np.nan])
In [13]: df
Out[13]:
a b c
0 1 NaN foo
1 3 2 nan
If you were assigning a numeric value, the following would work
In [16]: df['c'] = np.nan
In [17]: df.ix[0,'c'] = 1
In [18]: df
Out[18]:
a b c
0 1 NaN 1
1 3 2 NaN
Problem
I have this pandas dataframe: ``` d=pandas.DataFrame([{"a": 1}, {"a": 3, "b": 2}]) ``` and I'm trying to add a new column to it with non-null values only for certain rows, based on their numeric indices in the array. for example, adding a new column "c" only to the first row in `d`: ``` # array of row indices indx = np.array([0]) d.ix[indx]["c"] = "foo" ``` which should add "foo" as the column "c" value for the first row, and NaN for all other rows. but this doesn't seem to change the array: ``` d.ix[np.array([0])]["c"] = "foo" In [18]: d Out[18]: a b 0 1 NaN 1 3 2 ``` what am I doing wrong here? how can it be done? thanks.