0

I need to intiialize a column in a dataframe with an empty list. I followed this closely related question. That method uses np.empty. The difference is that my lists will use the append method to add strings.

df1['Qual']=np.empty((len(df1),0)).tolist()

I get the error:

File "/usr/local/lib/python3.6/dist-packages/pandas/core/series.py", line 2582, in append
    to_concat, ignore_index=ignore_index, verify_integrity=verify_integrity
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/reshape/concat.py", line 281, in concat
    sort=sort,
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/reshape/concat.py", line 357, in __init__
    raise TypeError(msg)
TypeError: cannot concatenate object of type '<class 'str'>'; only Series and DataFrame objs are valid

as soon as I try to do my first append with a string (this is inside a double loop):

df1['Qual'].loc[(df1['Ship '+shipid1[index1]]=='Yes') & (df1['Pref '+str(i1+1)]==preflevel1)].append([shipcodes1[index1]])

shipcodes1 is list of strings.

How do I create such a column?

cs95
  • 379,657
  • 97
  • 704
  • 746
user2751530
  • 197
  • 1
  • 2
  • 9
  • Can you share the line of code that gives this error? – emremrah Jun 01 '20 at 21:03
  • 1
    Please post the code you've run. – TayTay Jun 01 '20 at 21:03
  • Please show us your data and the final expected output, perhaps you don't need to iteratively append like you think. – cs95 Jun 01 '20 at 21:12
  • I do. This is a complicated enough problem as it is. The data in question is massive data set. The code I posted is definitely representative of what I need to do. – user2751530 Jun 02 '20 at 04:43
  • Tried out: https://stackoverflow.com/questions/57976658/error-while-trying-to-append-data-to-columns-in-python. The resulting lists are empty, when the logic of the program dictates that they should not be. I have done it by "hand" for a subset of the data to confirm. Weird. – user2751530 Jun 02 '20 at 05:53

0 Answers0