0

Coming along in the Pandas learning and am hitting a bit of a wall. My code is WORKING (thanks to you guys) but the output isn't "perfect".

I'd like to have the pandas output without the index being displayed AND the types not shown in my output. I've tried dropping the index from the read CSV, however, the csv is being read real-time from a web URL....which is the only thing I can think of as to what the issue is?

Code:

date_srch = "12/22/2021"
date_mask = pwrDF['Draw Date'] == date_srch
date_match = pwrDF.loc[date_mask, "Work Hours"]
date_match = date_match.str.replace(" ", ",")

if len(pwrDF[(pwrDF[ 'Draw Date' ] == date_srch)]) >0:
    print('The Work Hours for this date are ', date_match)
else:
    print('No work hours on this date')

Output: The Work Hours for this date are 182 07,16,19,48,68,15 Name: Winning Numbers, dtype: object

All I want is the following output: The Work Hours for this date are 07,16,19,48,68,15

VJ1222
  • 21
  • 1
  • To obtain just the values use `.values` at the end of the `.loc` expressionn. – user19077881 Mar 01 '23 at 15:28
  • Does this answer your question? [Remove name, dtype from pandas output of dataframe or series](https://stackoverflow.com/questions/29645153/remove-name-dtype-from-pandas-output-of-dataframe-or-series) – BigBen Mar 01 '23 at 15:29
  • With the .values gets me The work hours for this date are ['07 16 19 48 68 15'] – VJ1222 Mar 01 '23 at 15:59
  • 1
    How about, in addition to the `.values`, you have the following: `date_match = ", ".join(date_match[0].split(" ")` – Nick Flores Mar 01 '23 at 16:11

1 Answers1

0

Please provide more context. Is this the final output? Or are you trying to remove it for further processing? Regardless, below are the workings and answers on the assumption you are trying to show only output as a string. Please also provide more necessary codes next time, for example, the sample data.

Code:

import pandas as pd

data = {'Draw Date': ['02/13/2023', '02/10/2023', '02/06/2023','12/22/2021'],
        'Work Hours': ['10 14 30 40 51 01', '05 08 29 37 38 34', '17 22 36 37 52 24', '07 16 19 48 68 15']}
pwrDF = pd.DataFrame(data)

date_srch = "12/22/2021"
date_mask = pwrDF['Draw Date'] == date_srch
date_match = pwrDF.loc[date_mask, "Work Hours"]
date_match = date_match.str.replace(" ", ",")

#Delete any lines accordingly
if len(pwrDF[(pwrDF[ 'Draw Date' ] == date_srch)]) >0:
    date_match = str(date_match) #Turns date_match into string
    print("First output:\n"+'The Work Hours for this date are ', date_match + "\n") #Checks output
    eliminate1 = date_match.find(' ') #Find first spacing due to formatting, returns index
    eliminate2 = date_match.find('Name') #Find string "Name" due to formatting, returns index
    date_match = date_match[eliminate1+1:eliminate2-1] #Only select the strings after first spacing and before the string "Name" 
    print("Final output:")
    print('The Work Hours for this date are ', date_match)
else:
    print('No work hours on this date')

Output:

First output:
The Work Hours for this date are  3    07,16,19,48,68,15
Name: Work Hours, dtype: object

Final output:
The Work Hours for this date are     07,16,19,48,68,15
Hanpower
  • 1
  • 1
  • This is complicated compared with `date_match = pwrDF.loc[date_mask, "Work Hours"].values ; date_match = ", ".join(date_match[0].split(" "))` as in two comments above and does the same thing. – user19077881 Mar 01 '23 at 23:43