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?
CodePudding user response:
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)