我想要一个有条理的输出
for ind in df.index:
print(df['Name'][ind], df['Stream'][ind].rjust(15," "))
我做到了,但没有得到预期的输出
import pandas as pd
data = {'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka'],
'Age': [21, 19, 20, 18],
'Stream': ['Math', 'Commerce', 'Arts', 'Biology'],
'Percentage': [88, 92, 95, 70]}
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Stream', 'Percentage'])
print("Given Dataframe :\n", df)
print("\nIterating over rows using index attribute :\n")
for ind in df.index:
print(df['Name'][ind], df['Stream'][ind])
输出:
Ankit Math
Amit Commerce
Aishwarya Arts
Priyanka Biology
预期输出:
Ankit Math
Amit Commerce
Aishwarya Arts
Priyanka Biology
我希望我的输出像预期的输出一样,我已经尝试过正确对齐,但没有得到预期的输出;我怎么能得到这个?
答案 0 :(得分:0)
您是否考虑过将其打印为表格? 您需要导入以下内容:
from tabulate import tabulate
OR:
对于范围(10)中的i: 打印'%-12i%-12i'%(10 ** i,20 ** i)
答案 1 :(得分:0)
您可以指定要打印的每个项目的宽度。 在下面,我指定了10个字符的宽度来打印名称。
for ind in df.index:
print("{:10} {}".format(df['Name'][ind], df['Stream'][ind]) )