0

Is there something special about creating a DataFrame with strings that makes it not actually take the values? I'm at a loss as to how to even troubleshoot this.

df = pd.DataFrame()
df['Jan Total'] = '=SUM(B2:B32)'
df['Feb Total'] = '=SUM(C2:C32)'
... 
df['Yearly Total'] = '=SUM(B2:M32)'
df['Total in Gallons'] = '=SUM(B2:M32)/0.264172'

print(df)

Empty DataFrame
Columns: [Jan Total, Feb Total, Mar Total, April Total, May Total, June Total, July Total, Aug Total, Sep Total, Oct Total, Nov Total, Dec Total, Yearly Total, Total in Gallons]
Index: []
3
  • 3
    it assigns value to existing rows but you don't have any rows. You have to create at least one row to assign values - ie. df['Jan Total'] = ['=SUM(B2:B32)']. Commented Feb 21, 2020 at 16:43
  • @furas said it perfectly Commented Feb 21, 2020 at 16:48
  • BTW if you will have many rows then df['Jan Total'] = '=SUM(B2:B32)' will assign this value to all cells in column. ie. df = pd.DataFrame([[],[]]) df['Jan Total'] = '=SUM(B2:B32)' will put '=SUM(B2:B32)' in two rows. Commented Feb 21, 2020 at 16:49

1 Answer 1

3

Code

df['Jan Total'] = '=SUM(B2:B32)'

tries to replace values in all existing cells in column 'Jan Total' but you don't have rows so it can't replace them. It doesn't create new row.

You can assign value(s) using list

df['Jan Total'] = ['=SUM(B2:B32)']

df['Jan Total'] = ['=SUM(B2:B32)', 'value in second row']

You can also create DataFrame with row and then replace values

import pandas as pd

df = pd.DataFrame([[]])

df['Jan Total'] = '=SUM(B2:B32)'

df['Jan Total'] = '=SUM(B2:B32)'
df['Feb Total'] = '=SUM(C2:C32)'
... 
df['Yearly Total'] = '=SUM(B2:M32)'
df['Total in Gallons'] = '=SUM(B2:M32)/0.264172'

print(df)

Result:

      Jan Total     Feb Total  Yearly Total       Total in Gallons
0  =SUM(B2:B32)  =SUM(C2:C32)  =SUM(B2:M32)  =SUM(B2:M32)/0.264172

But if you have more rows - pd.DataFrame([[], []]) - then it will replace it in all rows

import pandas as pd

df = pd.DataFrame([[], []])

df['Jan Total'] = '=SUM(B2:B32)'

df['Jan Total'] = '=SUM(B2:B32)'
df['Feb Total'] = '=SUM(C2:C32)'
... 
df['Yearly Total'] = '=SUM(B2:M32)'
df['Total in Gallons'] = '=SUM(B2:M32)/0.264172'

print(df)

Result

      Jan Total     Feb Total  Yearly Total       Total in Gallons
0  =SUM(B2:B32)  =SUM(C2:C32)  =SUM(B2:M32)  =SUM(B2:M32)/0.264172
1  =SUM(B2:B32)  =SUM(C2:C32)  =SUM(B2:M32)  =SUM(B2:M32)/0.264172
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thank you! Incidentally, the fix also works if you just make the FIRST one a list. I put brackets around the string for January and voila - all of the subsequent ones worked because I had then established a row in that column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.