📌  相关文章
📜  如何在没有索引的情况下在Python中打印 Dataframe?

📅  最后修改于: 2022-05-13 01:55:26.405000             🧑  作者: Mango

如何在没有索引的情况下在Python中打印 Dataframe?

先决条件:熊猫

打印数据帧时,默认情况下,索引与输出一起出现,但如果需要,可以将其删除。要打印没有索引的数据帧,to_string() 中的索引参数必须设置为 False。

句法:

方法:

  • 导入模块
  • 创建一个简单的数据框。
  • 在 to_string() 中将索引设置为 false
  • 打印结果

下面给出使用这种方法的实现

示例 1:

Python3
import pandas as pd
 
df = pd.DataFrame({"Name": ["sachin", "sujay", "Amara", "shivam",
                            "Manoj"],
 
                   "Stream": ["Humanities", "Science", "Science",
                              "Commerce", "Humanities"]},
 
                  index=["A", "B", "C", "D", "E"])
 
print("THIS IS THE ORIGINAL DATAFRAME:")
display(df)
print()
 
print("THIS IS THE DATAFRAME  WITHOUT INDEX VAL")
print(df.to_string(index=False))


Python3
import pandas as pd
 
df = pd.DataFrame(["Geeks", "for", "geek"],
 
                  index=["A", "B", "C"])
 
print("THIS IS THE ORIGINAL DATAFRAME:")
display(df)
print()
 
print("THIS IS THE DATAFRAME  WITHOUT INDEX VAL")
print(df.to_string(index=False))


输出:

示例 2:

蟒蛇3

import pandas as pd
 
df = pd.DataFrame(["Geeks", "for", "geek"],
 
                  index=["A", "B", "C"])
 
print("THIS IS THE ORIGINAL DATAFRAME:")
display(df)
print()
 
print("THIS IS THE DATAFRAME  WITHOUT INDEX VAL")
print(df.to_string(index=False))

输出: