1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
DataBen
  • 13
  • 3
  • Possible duplicate of [Pandas apply ValueError: The truth value of a Series is ambigous](https://stackoverflow.com/questions/48460234/pandas-apply-valueerror-the-truth-value-of-a-series-is-ambigous) – venkata krishnan Nov 18 '19 at 06:35
  • 1
    The solution proposed in the linked issue is different to mine. Adding .values after .astype('str') resolves the issue. As @DYZ said, I have attempted to pass a series instead of a value. – DataBen Nov 18 '19 at 23:49
  • Apologies, as this is my first question on SO but how do I mark this as 'Answered'? EDIT: Never mind, I worked it out. :) – DataBen Nov 19 '19 at 00:04

1 Answers1

0

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)
DYZ
  • 55,249
  • 10
  • 64
  • 93