0

I am working with a piece of code, which is supposed to read the columns in each row of the data and based on the value of that cell, create a file name to pick. I was required to use the if elif else statements but the code sometimes picks the right value, sometimes not. So,

This is the data sample:

S.No     File refrence   Time         Detection_Location
1     5008_P1_272   2019-04-24 16:15:53      CV22
2     5007_P2_467   2019-04-25 19:00:22      CV23

The code I am using for this operation is:



# Converting columns to datetime format
df['Time'] = pd.to_datetime(df['Time'], errors = 'coerce').dt.date


#Stripping the  datetime
df['Time'] = pd.to_datetime(df['Time'])
df['Time'] = df['Time'].dt.strftime('%Y-%m-%d')


df[['Stripped Year', 'Stripped Month','Strip date']] = df['Time'].str.split(pat = "-", n=2, expand = True)

# Creating the file name string

for i in df.index:
    First = 'PHD'


    #for s in df['Stripped Year']:
    s = df.iloc[i]['Stripped Year']
    second1 = s[2:]
    print(second1)

    s1 = df.iloc[i]['Stripped Month']
    second2 = s1
    print(second2)
    if second2 == '02':
        second3 = '28'
    if second2 == '01' or '03' or '05' or '07' or '08' or '10' or '12':
        second3 = '31'
    if second2 == '04' or '06' or '09' or '11':
                second3 = '30'
    print(second3)


    s2 = df.iloc[i]['Detection_Location']
    if s2 == 'CV22':
        second4 = 'L1'
    elif s2 == 'CV23':
        second4 = 'L2'
    elif s2 == 'CV3':
        second4 = 'LC'
    print(second4)

    PHD_File_Name = First + ' ' +second1+second2+'01' + ' ' +second1+second2+second3 + ' ' + second4 + '_transformed' + '.xlsx'
    print(PHD_File_Name)

My code seems to work well in picking the month value( second3 value from second2 value) sometimes and sometimes it does not pick the right month. I used if elif else and if else else statement but the performance of the code is not consistent. Please help what am I doing wrong.

The desired output for the first row of the data should be:


second1 = 2019
second2 = 04
second3 = 30

Thanks

YatShan
  • 425
  • 2
  • 8
  • 22
zsh_18
  • 1,012
  • 1
  • 11
  • 29
  • You can use `isin()` to check the or conditions. Try `second2.isin(['01' ,'03' ,'05' ,'07' ,'08' ,'10' ,'12'])` Here is an example `df = pd.Series([10, 25, 3, 25, 24, 6])' This will give true if one item is in the series `any(df.isin([10,3]))` – XXavier Feb 18 '20 at 01:52
  • Be careful, this seems **really unidiomatic**. Have you read the Pandas docs? Why are you doing `df['Time'] = pd.to_datetime(df['Time'], errors = 'coerce').dt.date; df['Time'] = pd.to_datetime(df['Time']); df['Time'] = df['Time'].dt.strftime('%Y-%m-%d')` ? `df[['Stripped Year', 'Stripped Month','Strip date']] = df['Time'].str.split(pat = "-", n=2, expand = True)` is probably unnecessary, there are ways of getting specific elements of the date. – AMC Feb 18 '20 at 02:31
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – AMC Feb 18 '20 at 02:32
  • @AMC - No I am new to using pandas. I am just trying things to make my code robust and work right. Not sure if a few things are not important. Might be. – zsh_18 Feb 18 '20 at 02:42
  • _Not sure if a few things are not important. Might be._ I'm not sure I understand what you mean, can you elaborate? – AMC Feb 18 '20 at 02:44
  • @AMC - I am just saying that thanks for bringing to my knowledge that the first part of this code is not necessary. To the question of have I read pandas docs, I am saying no, I am a new learner of python and pandas and there might be some unnecessary piece in my code and that why I am here to learn from the world. – zsh_18 Feb 18 '20 at 02:54

1 Answers1

0

Your conditional statements are wrong. You cannot use

second2 == '01' or '03' or '05' or '07' or '08' or '10' or '12'

Each comparison need to be separate, like second2 == '01' or second2 == '03'

Johnson Zhou
  • 443
  • 3
  • 13