如何方法链 describe()、value_counts() 和 to_csv()

how to method chain describe(), value_counts(), and to_csv()

这是我的代码

#import modules
import pandas as pd

#assign pd.read_csv() to infile to read in data from a datafile
infile = pd.read_csv('..\Infiles\StarWars_Data.txt')

#get user input for selecting a Series name
Series_name = input("please enter the name of one of the series's for closer inspection")

#Select the Series
Series_Data = infile[Series_name]

#Chain the value_counts(), and describe() and to_csv() methods
Series_Data.value_counts()\
    .describe()\
    .to_csv('..\Outfiles\StarWars_Results.txt')

我希望它执行 value_counts()(returns 系列中唯一值的数量)、describe()(给出系列的汇总统计信息)和 to_csv(将存储的内容写入指定的 csv 文件)。

由于某种原因 to_csv() 正在返回 describe() 但它没有返回 value_counts(),我如何从 value_counts()describe() 写入数据到同一份文件?

IIUC,你要吗?

 pd.concat([df['words'].value_counts(),
            df['words'].describe()])\
   .to_csv('..\Outfiles\StarWars_Results.txt')

MCVE:

s = pd.Series([*'AABBBBCDDEEEEEEE'])

pd.concat([s.value_counts(), s.describe()]).rename_axis('key').to_csv('a.text')

!type a.txt

输出:

key,0
E,7
B,4
A,2
D,2
C,1
count,16
unique,5
top,E
freq,7