2

I'm trying to catch an empty list going into a pandas data frame but the above error occurs:

ValueError: Shape of passed values is (1, 5), indices imply (5, 5)

I currently have a simple set up here:

if not daily_info:
    daily_info= ["No data found today","No data found today","No data found today","No data found today","No data found today"]
    df = pd.DataFrame(data=daily_info, columns=['Send/Collect', 'Hospital', 'Courier', 'Kit', 'Manufacturer'])
    df = df.assign(Status="Not Started")

This may seem similar to this where it is asking about appending an existing DF however the answers don't really help me with a different context.

Can anyone help me as to where i've gone wrong?

Mohit Motwani
  • 4,662
  • 3
  • 17
  • 45
Colleen
  • 143
  • 1
  • 3
  • 14

1 Answers1

1

I think need pass nested list to DataFrame constructor - [daily_info]:

daily_info= ["No data found today","No data found today","No data found today","No data found today","No data found today"]
df = pd.DataFrame(data=[daily_info], columns=['Send/Collect', 'Hospital', 'Courier', 'Kit', 'Manufacturer'])
df = df.assign(Status="Not Started")
print (df)
          Send/Collect             Hospital              Courier  \
0  No data found today  No data found today  No data found today   

                   Kit         Manufacturer       Status  
0  No data found today  No data found today  Not Started 
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • It's bringing up this error now: "AssertionError: 5 columns passed, passed data had 4 columns" Despite the fact there's the right amount of amount of strings in the list - tried adding more out of miscounting and still says exactly the same with no numerical changes – Colleen Aug 21 '18 at 12:05
  • @Colleen - You have to match number of values of list with number of values in list for columns names, if difference it raise error. – jezrael Aug 21 '18 at 12:07
  • 1
    Nope I thought I missed something - your code works absolutely fine on its own however when adding it to the if statement the above error occurs @jezrael – Colleen Aug 21 '18 at 13:12
  • @Colleen - Can you explain more? `if not daily_info` seems wrong, it is `True` or `False` ? – jezrael Aug 21 '18 at 13:13
  • Sure @jezrael - it's simply a list that changes through the program. At that point it is empty, so False and that's when it's erroring – Colleen Aug 21 '18 at 13:14
  • @Colleen - How working `if not len(daily_info) == 0:` ? – jezrael Aug 21 '18 at 13:17
  • Or `if len(daily_info) != 0:` – jezrael Aug 21 '18 at 13:17
  • the same occurs except the number changes in the error. With both those alternatives it is "AssertionError: 5 columns passed, passed data had 0 columns" – Colleen Aug 21 '18 at 13:21
  • Use `if len(daily_info) == 5` because there is `5` columns before assign (it add 6.th column) – jezrael Aug 21 '18 at 13:22
  • Do you mean extending the if statement as to be `if len(daily_info) == 5: (call pandas statement) else: (reassign daily info then call pandas)` because if that is the case the logic is flawed as the length of daily info changes whilst the columns stay constant (it becomes a nested list) – Colleen Aug 21 '18 at 13:27
  • Yes, if `daily_info` is list solution working if matched number of columns with number of values in list `daily_info`, so `if len(daily_info) == 5:` is necessary. – jezrael Aug 21 '18 at 13:28
  • daily_info is a nested list so i think the logic doesn't quite work there. Because we are checking for how many columns doesn't `try: len(daily_info[1]) == 5` work better logically? Because the length of the list changes – Colleen Aug 21 '18 at 13:38
  • The problem with that is it still returns that old error @jezrael – Colleen Aug 21 '18 at 13:39
  • @Colleen - So can you change `daily_info` with nested data? I think [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve). – jezrael Aug 21 '18 at 13:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178453/discussion-between-colleen-and-jezrael). – Colleen Aug 21 '18 at 13:44