简体   繁体   English

如何将多列打印为数据框 - python

[英]how to print multiple columns as a data frame - python

I need to print 2 specific columns as a data frame and not a series.我需要将 2 个特定列打印为数据框而不是系列。

this question is different from when df1 is printed it returns a series.这个问题与打印 df1 时不同,它返回一个系列。 movie title and movie year are the columns I need to print.电影标题和电影年份是我需要打印的列。 I used the df = my_series.to_frame() function but it still returns a series.我使用了 df = my_series.to_frame() function 但它仍然返回一个系列。

def find_movie_by_year():
    x = input("enter movie year")
    d = df[df["title_year"] == int(x)]
    df1 = d[["movie_title","title_year"]]
    print('''

    ''')
    print(df1)

EDIT 1:编辑1:

this is the output format i need:这是我需要的 output 格式:

在此处输入图像描述

this is the output format i get from the code:这是我从代码中得到的 output 格式:

在此处输入图像描述

You can use display function instead of simply printing it.您可以使用显示function 而不是简单地打印它。

def find_movie_by_year():
x = input("enter movie year")
d = df[df["title_year"] == int(x)]
df1 = d[["movie_title","title_year"]]
print('''

''')
#print(df1)
display(df1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python中如何将数据框从多列变成单列 - How to bring data frame into single column from multiple columns in python 如何获取Python数据框中多列的斜率 - how to get the slope of multiple columns in Python data frame Pyspark / Python数据框多列不等于 - Pyspark/Python data frame multiple columns not equal to python将数据框列拆分为多行 - python split data frame columns into multiple rows python 如何将一个 pandas 数据帧中的值计数转置到第二个数据帧中的多列? - python How to transpose the count of values in one pandas data frame to multiple columns in a second data frame? Python Pandas:如何打印数据框中所有值为0的所有列 - Python Pandas: How to print all columns in the data frame where all the values are 0 熊猫,如何使用多个分组列替换python数据框中的平均值 - Pandas, how to replace mean values in python data frame using multiple grouped columns Grouby并在Python的多列数据框中填充缺少的月份 - Grouby and fill missing months in multiple columns data frame in Python Python数据框将过滤器应用于条件相同的多列? - Python data frame apply filter on multiple columns with same condition? 将熊猫数据框的多列转换为虚拟变量-Python - Convert multiple columns of a pandas data frame to dummy variables - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM