Home > OS >  Is there a way to have insertion of tags or labels with respect to a certain value range?
Is there a way to have insertion of tags or labels with respect to a certain value range?

Time:09-26

I have this data and would like to insert a new column titled 'Level'. I understand 'insert' is a mode of entering in a new column. I tried an 'if' for the argument 'value', but this is not yielding anything.

Data:

                                         Active  Discharged  Deaths
State/UTs                                                           
Andaman and Nicobar                            6        7437     129
Andhra Pradesh                             14550     1993589   13925
Arunachal Pradesh                            634       52507     267
Assam                                       6415      580491    5710
Bihar                                         55      716048    9656
Chandigarh                                    35       64273     814
Chhattisgarh                                 354      990757   13557
Dadra and Nagar Haveli and Daman and Diu       2       10659       4
Delhi                                        367     1412542   25082
Goa                                          885      170391    3210
Gujarat                                      152      815275   10082
Haryana                                      617      760271    9685
Himachal Pradesh                            1699      209420    3613
Jammu and Kashmir                           1286      320337    4410
Jharkhand                                    126      342716    5133
Karnataka                                  17412     2901299   37426
Kerala                                    239338     3966557   21631
Ladakh                                        54       20327     207
Lakshadweep                                    9       10288      51
Madhya Pradesh                               125      781629   10516
Maharashtra                                51234     6300755  137811
Manipur                                     3180      110602    1802
Meghalaya                                   2104       73711    1329
Mizoram                                    11414       54056     226
Nagaland                                     712       29045     631
Odisha                                      6322      997790    8055
Puducherry                                   914      121452    1818
Punjab                                       326      584079   16444
Rajasthan                                     86      945097    8954
Sikkim                                       913       28968     375
Tamil Nadu                                 16256     2572942   35036
Telengana                                   5505      650453    3886
Tripura                                      691       81866     803
Uttar Pradesh                                227     1686369   22861
Uttarakhand                                  379      335358    7388
West Bengal                                 8480     1525581   18515

code:

data = Table.read_table('IndiaStatus.csv')#.drop('Discharged', 'Discharge Ratio (%)','Total Cases','Active','Deaths')


data2.info()

data3 = data2.set_index("State/UTs")

data3 = data3[["Active","Discharged","Deaths"]]
print(data3)

data3.insert(1, column = "Level", value = "Severe" if data3["Active"] > 91874)

output:

line 49
    data3.insert(1, column = "Level", value = "Severe" if data3["Active"] > 91874)
                                                                                 ^
SyntaxError: invalid syntax

CodePudding user response:

The SyntaxError is because you need a else condition, so something like value = "Severe", if data3["Active"] > 91874 else 'OTHER' would remove the error. That said, it won't work in this case and return another error of using a Series - in this case data3["Active"] > 91874 - in a if statement.

I believe you can use np.where here

data3.insert(1, column = "Level", 
             value = np.where(data3["Active"] > 91874, "Severe", 'OTHER')

Replace OTHER in the above code by any value you want to assign in the column when the condition data3["Active"] > 91874 is not met

  • Related