Error while transforming series with series.map() function - pandas

def locate (code):
string1 = str(code)
floor = string1[3]
if floor == '1':
return 'Ground Floor'
else:
if int(string1[5]) < 1:
lobby = 'G'
elif int(string1[5]) < 2:
lobby = 'F'
else:
lobby = 'E'
return floor + lobby
print(locate('S191009'))
print(locate('S087525'))
This function works fine with individual input code as above with Output
Ground Floor
7E
But when I use this to map a series in data frame, it shows error.
error_data1['location'] = error_data1['status'].map(locate)
Error message: string index out of range.
How can I fix this??

Your problem is with your series values:
se = pd.Series(['S191009', 'rt'])
se.map(locate)
produces the same error you reported. You can ignore these rows using try...except in function if it does not hurt you.

The problem is you are indexing an index on a string that doesn't exist (i.e the string is shorter than what you expect). As the other answer mentioned, if you try and use
my_string="foo"
print(my_string[5])
You will get the same error. To solve this you should add a try except statement, or for simplicity an initial if statement that returns "NotValid" or something like that. Your data probably has strings that do not follow the standard form you expect.

Related

TypeError: '<' not supported between instances of 'int' and 'Timestamp'

I am trying to change the product name when the period between the expiry date and today is less than 6 months. When I try to add the color, the following error appears:
TypeError: '<' not supported between instances of 'int' and 'Timestamp'.
Validade is the column where the products expiry dates are in. How do I solve it?
epi1 = pd.read_excel('/content/timadatepandasepi.xlsx')
epi2 = epi1.dropna(subset=['Validade'])`
pd.DatetimeIndex(epi2['Validade'])
today = pd.to_datetime('today').normalize()
epi2['ate_vencer'] = (epi2['Validade'] - today) /np.timedelta64(1, 'M')
def add_color(x):
if 0 <x< epi2['ate_vencer']:
color='red'
return f'background = {color}'
epi2.style.applymap(add_color, subset=['Validade'])
Looking at your data, it seems that you're subtracting two dates and using this result inside your comparison. The problem is likely occurring because df['date1'] - today returns a pandas.Series with values of type pandas._libs.tslibs.timedeltas.Timedelta, and this type of object does not allow you to make comparisons with integers. Here's a possible solution:
epi2['ate_vencer'] = (epi2['Validade'] - today).dt.days
# Now you can compare values from `"ate_vencer"` with integers. For example:
def f(x): # Dummy function for demonstration purposes
return 0 < x < 10
epi2['ate_vencer'].apply(f) # This works
Example 1
Here's a similar error to yours, when subtracting dates and calling function f without .dt.days:
Example 2
Here's the same code but instead using .dt.days:

Start working on pandas and getting erroe on thos

I have just started my work on pandas. Currently I'm working on a dataset of NETFLIX.
In this dataset I want to add a new column which contains the total number of cast members in that particular movie or tv show. I can calculate the cast individually but I want to calculate all of them. Can someone help me to write this code ?
Here is what I'm trying to do:
link https://www.kaggle.com/datasets/shivamb/netflix-shows?
def set_cast(val):
if val is None:
return 0
if val == 'None':
return 0
return len(val.split(', '))
data['num_of_cast'] = data['cast'].apply(set_cast)
getting these error
return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
'float' object has no attribute 'split'
In your dataset the first column are some statistics. Maybe they are the flout values that causes the error.
Try skipping the first column. Maybe like this:
data['num_of_cast'] = [set_cast(x) for x in data['cast'] if data['cast'].index(x) >=1]
Instead of this:
data['num_of_cast'] = data['cast'].apply(set_cast)

How do I reverse each value in a column bit wise for a hex number?

I have a dataframe which has a column called hexa which has hex values like this. They are of dtype object.
hexa
0 00802259AA8D6204
1 00802259AA7F4504
2 00802259AA8D5A04
I would like to remove the first and last bits and reverse the values bitwise as follows:
hexa-rev
0 628DAA592280
1 457FAA592280
2 5A8DAA592280
Please help
I'll show you the complete solution up here and then explain its parts below:
def reverse_bits(bits):
trimmed_bits = bits[2:-2]
list_of_bits = [i+j for i, j in zip(trimmed_bits[::2], trimmed_bits[1::2])]
reversed_bits = [list_of_bits[-i] for i in range(1,len(list_of_bits)+1)]
return ''.join(reversed_bits)
df['hexa-rev'] = df['hexa'].apply(lambda x: reverse_bits(x))
There are possibly a couple ways of doing it, but this way should solve your problem. The general strategy will be defining a function and then using the apply() method to apply it to all values in the column. It should look something like this:
df['hexa-rev'] = df['hexa'].apply(lambda x: reverse_bits(x))
Now we need to define the function we're going to apply to it. Breaking it down into its parts, we strip the first and last bit by indexing. Because of how negative indexes work, this will eliminate the first and last bit, regardless of the size. Your result is a list of characters that we will join together after processing.
def reverse_bits(bits):
trimmed_bits = bits[2:-2]
The second line iterates through the list of characters, matches the first and second character of each bit together, and then concatenates them into a single string representing the bit.
def reverse_bits(bits):
trimmed_bits = bits[2:-2]
list_of_bits = [i+j for i, j in zip(trimmed_bits[::2], trimmed_bits[1::2])]
The second to last line returns the list you just made in reverse order. Lastly, the function returns a single string of bits.
def reverse_bits(bits):
trimmed_bits = bits[2:-2]
list_of_bits = [i+j for i, j in zip(trimmed_bits[::2], trimmed_bits[1::2])]
reversed_bits = [list_of_bits[-i] for i in range(1,len(list_of_bits)+1)]
return ''.join(reversed_bits)
I explained it in reverse order, but you want to define this function that you want applied to your column, and then use the apply() function to make it happen.

Conditional If Statement: If value in row starts with letter in string … set another column with some corresponding value

I have the 'Field_Type' column filled with strings and I want to derive the values in the 'Units' column using an if statement.
So Units shows the desired result. Essentially I want to call out what type of activity is occurring.
I tried to do this using my code below but it won't run (please see screen shot below for error). Any help is greatly appreciated!
create_table['Units'] = pd.np.where(create_table['Field_Name'].str.startswith("W"), "MW",
pd.np.where(create_table['Field_Name'].str.contains("R"), "MVar",
pd.np.where(create_table['Field_Name'].str.contains("V"), "Per Unit")))```
ValueError: either both or neither of x and y should be given
You can write a function to define your conditionals, then use apply on the dataframe and pass the funtion
def unit_mapper(row):
if row['Field_Type'].startswith('W'):
return 'MW'
elif 'R' in row['Field_Type']:
return 'MVar'
elif 'V' in row['Field_Type']:
return 'Per Unit'
else:
return 'N/A'
And then
create_table['Units'] = create_table.apply(unit_mapper, axis=1)
In your text you talk about Field_Type but you are using Field_Name in your example. Which one is good ?
You want to do something like:
create_table[create_table['Field_Type'].str.startwith('W'), 'Units'] = 'MW'
create_table[create_table['Field_Type'].str.startwith('R'), 'Units'] = 'MVar'
create_table[create_table['Field_Type'].str.startwith('V'), 'Units'] = 'Per Unit'

VB.Net Enum ToString returns an unknown number

I built a simple vb.net winforms project that pings IP addresses and logs the results. It works fine on most machines I've tried it on. I log the status result of the ping (System.Net.NetworkInformation.IPStatus) by using the IPStatus.tostring method.
Normally this returns a text result such as "Success" or "TimedOut"
Yesterday, on one machine it returned "65" ...which is not one of the enum values. I have a feeling it might be a combination of values. I ran some test code:
Dim status As System.Net.NetworkInformation.IPStatus
status = Net.NetworkInformation.IPStatus.Success
MsgBox(status.ToString)
Which returns "Success"
And this:
status = Net.NetworkInformation.IPStatus.BadDestination Or Net.NetworkInformation.IPStatus.BadHeader
MsgBox(status.ToString)
Which returns "11050"
I suspect the "65" I saw was the result of some combination of enum values. Is there any way I can change the code in my second example to show the text names of both values? That is... any way I can see ALL values in this variable?
IPStatus is NOT a Flags enum, therefore it is not appropriate to combine its member values in this way. This is its definition via Reflector:
Public Enum IPStatus
' Fields
BadDestination = &H2B0A
BadHeader = &H2B22
BadOption = &H2AFF
BadRoute = &H2B04
DestinationHostUnreachable = &H2AFB
DestinationNetworkUnreachable = &H2AFA
DestinationPortUnreachable = &H2AFD
DestinationProhibited = &H2AFC
DestinationProtocolUnreachable = &H2AFC
DestinationScopeMismatch = &H2B25
DestinationUnreachable = &H2B20
HardwareError = &H2B00
IcmpError = &H2B24
NoResources = &H2AFE
PacketTooBig = &H2B01
ParameterProblem = &H2B07
SourceQuench = &H2B08
Success = 0
TimedOut = &H2B02
TimeExceeded = &H2B21
TtlExpired = &H2B05
TtlReassemblyTimeExceeded = &H2B06
Unknown = -1
UnrecognizedNextHeader = &H2B23
End Enum
How you are getting an IPStatus value of 65 - now that's the real question :)
Try using System.Enum to get the name of the value.
In your example, use:
MsgBox(System.Enum.GetName(GetType(Net.NetworkInformation.IPStatus), status))
It looks like apart from Success (0) and Unknown (-1), the defined enum values range from 11002 to 11045, so 65 is not a combination of any of the defined enum values.
If you are getting 65 back, you will not be able to resolve this to a string.
That enum is not marked with the FlagsAttribute and therefore should not be or'd together because the result could overlap. You are better off creating your own enum to contain the values you are looking for.
You should be able to loop over the enum's range using something like [1], test whether the current enum bit is represented in the value and add it to a string builder.
I find it hard to write up a working sample in VB.NET in this little text box, but I'm sure someone else will oblige.
[1] http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c