'Why doesnt to_string() work for my pandas calculation?

def total_incidents_created(self):
    df = pd.read_csv("poam.csv", encoding='windows-1252', parse_dates=True)
    df = df.Completed.value_counts().Assigned
    df = df.to_string()
    print(df)

i keep getting an error AttributeError: numpy.int64' object has no attribute 'to_strin

the variable has an integer 10 assigned to it...



Solution 1:[1]

You seem to be looking for something like the following and seems also that your mistake was to write df.value_counts.assigned.to_string(), instead of df.assigned.value_counts().to_string()

import pandas as pd
# This will be a small DataFrame example with an 'assigned' column
df = pd.DataFrame(([0,'zero'], [1, 'one'], [2,'one']), columns=['col', 'assigned'])

df.head()
Out[22]: 
   col assigned
0    0     zero
1    1      one
2    2      one

# the output of value_counts()
df.assigned.value_counts()
Out[23]: 
one     2
zero    1
Name: assigned, dtype: int64

# The above output converted to string
df.assigned.value_counts().to_string()
Out[24]: 'one     2\nzero    1'

# and the above printed
print(df.assigned.value_counts().to_string())
one     2
zero    1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yannis P.