6

I am having trouble list of lists to pandas DataFrame.

here is my data:

list_of_lists = np.array(hazard)
[['13-06-2016' '2.0' '1.0' '3.0' '88.0' '0.0' '72.0' '7.27']
 ['18-06-2016' '1.0' '0.0' '3.0' '85.5' '0.0' '77.0' '8.05']
 ['22-06-2016' '3.0' '0.0' '5.0' '91.5' '0.0' '66.0' '7.54']
 ['26-06-2016' '3.0' '2.0' '4.0' '89.6' '1.0' '74.0' '10.0']
 ['01-07-2016' '3.0' '0.0' '1.0' '88.9' '0.0' '72.0' '6.75']
 ['27-08-2016' '7.0' '4.0' '2.0' '81.8' '2.0' '91.0' '8.79']
 ['01-09-2016' '0.0' '0.0' '1.0' '59.3' '1.0' '46.0' '6.92']
 ['11-09-2016' '2.0' '1.0' '4.0' '91.7' '0.0' '71.0' '6.84']
 ['16-09-2016' '0.0' '0.0' '1.0' '81.8' '1.0' '68.0' '7.07']
 ['24-09-2016' '2.0' '0.0' '1.0' '84.2' '0.0' '52.0' '6.11']
 ['30-10-2016' '3.0' '3.0' '5.0' '83.0' '1.0' '72.0' '8.87']
 ['05-11-2016' '3.0' '3.0' '1.0' '94.6' '0.0' '75.0' '9.76']
 ['09-11-2016' '1.0' '1.0' '4.0' '92.1' '0.0' '84.0' '7.21']
 ['20-11-2016' '0.0' '0.0' '5.0' '84.6' '1.0' '92.0' '8.27']
 ['26-11-2016' '2.0' '1.0' '1.0' '81.8' '0.0' '46.0' '7.19']
 ['26-12-2016' '5.0' '2.0' '4.0' '85.7' '1.0' '87.0' '10.0']
 ['31-12-2016' '2.0' '1.0' '5.0' '86.0' '1.0' '65.0' '7.78']
 ['04-01-2017' '2.0' '0.0' '1.0' '83.6' '1.0' '81.0' '6.07']]

and I have a pandas dataframe that I have constructed as

player_df = pd.DataFrame(columns=['Date', 'Total Shots', 'On Target',
                                      'Key Passes', 'Passing Accuracy', 'Aerials Won',
                                      'Touches', 'WhoScored Rating'])

now when I try player_df.append(list_of_lists) I get,

TypeError: cannot concatenate a non-NDFrame object.

can someone tell me what is going wrong here? the length of the lists matches with the number of columns here so surely it should be fine?

i.n.n.m
  • 2,936
  • 7
  • 27
  • 51
entercaspa
  • 674
  • 2
  • 7
  • 19

1 Answers1

23

The error is telling you that you need a pandas NDFrame object which most commonly is a DataFrame. Try converting your list of lists to a dataframe and then appending

player_df.append(pd.DataFrame(list_of_lists, columns=player_df.columns))
Ted Petrou
  • 59,042
  • 19
  • 131
  • 136
  • Correct as always. Thanks! – Ted Petrou Jan 05 '17 at 23:25
  • 2
    Important to remember that this will not modify `player_df` in place, but only return the appended df. – Ron Kalian Mar 17 '21 at 11:27
  • Quoting [this reply](https://stackoverflow.com/a/75956237/13083583): As of pandas 2.0, `append` (previously deprecated) [was removed](https://pandas.pydata.org/docs/whatsnew/v2.0.0.html#removal-of-prior-version-deprecations-changes). You need to use [`concat`](https://pandas.pydata.org/docs/reference/api/pandas.concat.html) instead (for most applications) – do-me May 05 '23 at 14:17