Trying to take a value from a pandas dataframe and insert it into a document using python-docx:
import docx
from docx.shared import Inches
import numpy as np
import pandas as pd
df = pd.DataFrame({'A':(1,2,3,4,5),'B':('a','b','c','d','e')})
document = docx.Document()
p = document.add_paragraph(df.loc[df.B=='c', 'A'].astype('str'))
but I get this error:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I have found many references on stack overflow to this issue but its not clear how my situation relates as my code only returns one result both in this re-runable example and in actual data. I have tried several variants of df.loc[df.B=='c', 'A']
without a different result.
Edit: possible duplicate referenced below relates to logical arguments. In my case the issue was I was attempting to use a series in place of a value. Solution is outlined below by DYZ.
I python-docx seems to dislike the float data type so I tried converting to a string but naturally that didn't resolve the issue.
EDIT: python-docx requires a string only.
#This works:
p = document.add_paragraph('1')
#This doesn't:
p = document.add_paragraph(1)
#however the error is unrelated to the issue highlighted above.
I would appreciate it if someone could highlight to me what I'm doing wrong as this has me stumped.
I installed python-docx this morning so it is the latest version i.e. python-docx-0.8.10.
Method document.add_paragraph
requires a string (you should have read the documentation). Instead, you pass a pandas Series. If you want to add more than one paragraph, you need a loop.
paragraphs = df.loc[df.B=='c', 'A'].astype('str').values
for paragraph in paragraphs:
document.add_paragraph(paragraph)