So I have initialized an empty pandas DataFrame and I would like to iteratively append lists (or Series) as rows in this DataFrame. What is the best way of doing this?
The simplest way:
my_list = [1,2,3,4,5]
df['new_column'] = pd.Series(my_list).values
Edit:
Don't forget that the length of the new list should be the same of the corresponding Dataframe.
Here's a simple and dumb solution:
>>> import pandas as pd
>>> df = pd.DataFrame()
>>> df = df.append({'foo':1, 'bar':2}, ignore_index=True)
If you want to add a Series and use the Series' index as columns of the DataFrame, you only need to append the Series between brackets:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame()
In [3]: row=pd.Series([1,2,3],["A","B","C"])
In [4]: row
Out[4]:
A 1
B 2
C 3
dtype: int64
In [5]: df.append([row],ignore_index=True)
Out[5]:
A B C
0 1 2 3
[1 rows x 3 columns]
Whitout the ignore_index=True
you don't get proper index.
simply use loc:
>>> df
A B C
one 1 2 3
>>> df.loc["two"] = [4,5,6]
>>> df
A B C
one 1 2 3
two 4 5 6
Overview: you can use loc to append a list to an existing dataframe
win_results_df=pd.DataFrame(columns=['GameId',
'Team',
'TeamOpponent',
'HomeScore',
'VisitorScore','Target'])
for key,item in teams.items():
game_filter=(games_df['homeDisplayName']==key)
win_df=games_df[game_filter]['HomeScore']>games_df[game_filter]['VisitorScore']
for oppKey,teamOpponent in games_df[game_filter][win_df].iterrows():
df_length = len(win_results_df)
win_results_df.loc[df_length] = [teamOpponent['gameId'],key,teamOpponent['visitorDisplayName'],teamOpponent['HomeScore'],teamOpponent['VisitorScore'],True]
Following onto Mike Chirico's answer... if you want to append a list after the dataframe is already populated...
>>> list = [['f','g']]
>>> df = df.append(pd.DataFrame(list, columns=['col1','col2']),ignore_index=True)
>>> df
col1 col2
0 a b
1 d e
2 f g