Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

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.

this question is different from when df1 is printed it returns a series. 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.

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:

this is the output format i need:

enter image description here

this is the output format i get from the code:

enter image description here

question from:https://stackoverflow.com/questions/65859714/how-to-print-multiple-columns-as-a-data-frame-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use display function instead of simply printing it.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...