Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Remove name, dtype from pandas output of dataframe or series

I have output file like this from a pandas function.

Series([], name: column, dtype: object)
311     race
317     gender
Name: column, dtype: object

I'm trying to get an output with just the second column, i.e.,

race
gender

by deleting top and bottom rows, first column. How do I do that?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

DataFrame/Series.to_string

These methods have a variety of arguments that allow you configure what, and how, information is displayed when you print. By default Series.to_string has name=False and dtype=False, so we additionally specify index=False:

s = pd.Series(['race', 'gender'], index=[311, 317])

print(s.to_string(index=False))
#   race
# gender

If the Index is important the default is index=True:

print(s.to_string())
#311      race
#317    gender

Series.str.cat

When you don't care about the index and just want the values left justified cat with a ' '. Values need to be strings, so convert first if necessary.

#s = s.astype(str)

print(s.str.cat(sep='
'))
#race
#gender

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...