Search code examples
python-3.xpandasserieschaining

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


Here is my code

#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')

I expect it to perform value_counts() (returns the numbers of unique values in a series), describe() (gives summary statistics on a series), and to_csv(writes what is stored into a specified csv file).

For some reason to_csv() is returning describe() but it is not returning value_counts(), how do I write the data from both value_counts() and describe() to the same document?


Solution

  • IIUC, do you want?

     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
    

    Output:

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