如何使用 Python 打印 pandas 数据框的各行?

How to print individual rows of a pandas dataframe using Python?

Python.

的新手

我正在尝试从数据框中提取数据并将其放入字符串中以打印到 docx 文件。

这是我当前的代码:


add_run("Placeholder A").italic= True

for i in range(0, list(df.shape)[0]):
    A = df.iloc[findings][0]
    B = df.iloc[findings][1] 
    C =df.iloc[findings][2] 
output = ('The value of A: {}, B: {}, C: {}').format(A,B,C)
doc.add_paragraph(output)

我追求的输出是:

占位符 A

占位符 A

当前正在打印占位符 A 下数据框的所有输出。

我哪里出错了?

Here (Whosebug - How to iterate over rows in a DataFrame in Pandas?) 您可以找到迭代 pandas 数据帧行的帮助。剩下要做的就是 print(row) :)

编辑:

这是打印先前创建的数据框中的行的代码示例(根据 link 的回答制作):

import pandas as pd

inp = [{'c1': 10, 'c2': 100, 'c3': 100}, {'c1': 11, 'c2': 110, 'c3': 100}, {'c1': 12, 'c2': 120, 'c3': 100}]
df = pd.DataFrame(inp)

for index, row in df.iterrows():
    A = row["c1"]
    B = row["c2"]
    C = row["c3"]
    print('The value of A: {}, B: {}, C: {}'.format(A, B, C))