0

I have a pandas dataframe. I have a column that could potentially have null values or an array of string values in it. But I'm having trouble working out how to store values in this column.

This is my code now:

df_completed = df[df.completed]
df['links'] = None
for i, row in df_completed.iterrows():
    results = get_links(row['nct_id'])
    if results:
        df[df.nct_id == row['nct_id']].links = results
        print df[df.nct_id == row['nct_id']].links

But this has two problems:

  • When results is an array of length 1, the printed output is None, rather than the array, so I think I must be saving the value wrong
  • When results is a longer array, the line where I save the value produces an error: ValueError: Length of values does not match length of index

What am I doing wrong?

Richard
  • 62,943
  • 126
  • 334
  • 542

1 Answers1

1

I am not sure it's advisable to try to store arrays in pandas like this, have you considered trying to serialise the array contents and then store?

If storing an array is what you're after anyways, then you can try with the set_value() method, like this (make sure you take care of the dtype of column nct_id):

In [35]: df = pd.DataFrame(data=np.random.rand(5,5), columns=list('ABCDE'))

In [36]: df
Out[36]: 
          A         B         C         D         E
0  0.741268  0.482689  0.742200  0.210650  0.351758
1  0.798070  0.929576  0.522227  0.280713  0.168999
2  0.413417  0.481230  0.304180  0.894934  0.327243
3  0.797061  0.561387  0.247033  0.330608  0.294618
4  0.494038  0.065731  0.538588  0.095435  0.397751

In [38]: df.dtypes
Out[38]: 
A    float64
B    float64
C    float64
D    float64
E    float64
dtype: object

In [39]: df.A = df.A.astype(object)

In [40]: df.dtypes
Out[40]: 
A     object
B    float64
C    float64
D    float64
E    float64
dtype: object

In [41]: df.set_value(0, 'A', ['some','values','here'])
Out[41]: 
                      A         B         C         D         E
0  [some, values, here]  0.482689  0.742200  0.210650  0.351758
1               0.79807  0.929576  0.522227  0.280713  0.168999
2              0.413417  0.481230  0.304180  0.894934  0.327243
3              0.797061  0.561387  0.247033  0.330608  0.294618
4              0.494038  0.065731  0.538588  0.095435  0.397751

I hope this helps!

Thanos
  • 2,472
  • 1
  • 16
  • 33