martorad
martorad

Reputation: 165

Pandas concat() doesn't work, without throwing any errors?

I am trying to append some data to an empty Pandas Series in a loop. I do this as follows:

i = 0
values = pd.Series(dtype=int)
for i in range(1, 200):
    temp = pd.Series([int(df_train.iloc[[0],[i]].values)])
    pd.concat([values, temp])
    i += 1
values

I create a temporary Pandas Series to store a value from my dataframe at a specific index (In particular I am interested in the column values 1-200 of the first row). This returns:

Series([], dtype: int32)

Why does this fail? Why are there no errors raised?

Upvotes: 1

Views: 481

Answers (1)

jezrael
jezrael

Reputation: 862521

IIUC need assign pd.concat:

values = pd.Series(dtype=int)
for i in range(1, 200):
    temp = pd.Series([int(df_train.iloc[[0],[i]].values)])
    values = pd.concat([values, temp])

Better is create list of Series annd concat outside loop:

L = []
for i in range(1, 200):
    temp = pd.Series([int(df_train.iloc[[0],[i]].values)])
    L.append(temp)

values = pd.concat(L)
print (values)

L = [pd.Series([int(df_train.iloc[[0],[i]].values)] for i in range(1, 200)]
values = pd.concat(L)
print (values)

Upvotes: 1

Related Questions