-1

I have a pandas dataframe built using (value_counts) function. When I call that dataframe in the body of the email it gets displayed as below

Updated view of the dataframe:

  Exact match   169
1 Missing       121
2 Dropped by 1% 1

I would like to have the header (or the first row) to be empty or have a title but have the first row pushed to the second row in the dataframe. Given below is the syntax:

SumMail = pd.value_counts(Output1['Status'].values)

Given below is the view of Output1 dataframe:

      Status
0 ABC Exact match
1 DEF Dropped by 1%
2 XYZ Exact match

Could anyone advice.

OriolAbril
  • 7,315
  • 4
  • 29
  • 40
Taukheer
  • 1,091
  • 2
  • 13
  • 20
  • 1
    Please post your dataframe as text, not as an image. – user3483203 Apr 19 '18 at 13:57
  • Have you tried removing the `.values`? Its hard to tell how to solve it without knowing what is `Output1` – OriolAbril Apr 19 '18 at 14:00
  • I would suggest trying inserting a row of `[np.nan, np.nan]` at the 0th position (https://stackoverflow.com/questions/24284342/insert-a-row-to-pandas-dataframe) – David L Apr 19 '18 at 14:14

1 Answers1

0

The function pd.value_counts takes a Series as input. Therefore, using a .values removes the column name info and converts it to an array, which is why it uses the 1st row as column name afterwards. Removing the .values will solve that.

Example

Output1 = pd.DataFrame({'':['a','b','c','d','e'],'Status':['Exact match','Dropped by 1%','Exact match','Missing','Exact match']})
print(Output1)
#              Status
# 0  a    Exact match
# 1  b  Dropped by 1%
# 2  c    Exact match
# 3  d        Missing
# 4  e    Exact match

# First option
print(pd.value_counts(Output1['Status']))
# Exact match      3
# Missing          1
# Dropped by 1%    1
# Name: Status, dtype: int64 # Note the Name still present

# Second option
print(pd.DataFrame(pd.value_counts(Output1['Status'])))
#                    Status
# Exact match         3
# Missing             1
# Dropped by 1%       1
OriolAbril
  • 7,315
  • 4
  • 29
  • 40
  • Hey xg.plt.py, I did remove ".values" from the code but the output returned was in the same format... – Taukheer Apr 19 '18 at 14:34
  • How are you printing `SumMail`? – OriolAbril Apr 19 '18 at 15:19
  • Thanks xg, I am able to have a header column and the results are shown from row 2 onwards. However, how can I rename the labels in the header columns. Given below is the view of my dataframe ,Unnamed:0,Status 0,Missing,3 1,Exact match,1 2,NaN,1 I have tried using rename function but still no luck, could you please help. Thanks.. – Taukheer Apr 20 '18 at 05:50
  • @Taukheer Have you tried what is explained [here](https://stackoverflow.com/questions/11346283/renaming-columns-in-pandas)? If it does not work for you then it's probably worth another quesion for this. – OriolAbril Apr 20 '18 at 10:02