Change entire pandas Series based on conditions - python

In my pandas DataFrame I want to add a new column (NewCol), based on some conditions that follow from data of another column (OldCol).
To be more specific, my column OldCol contains three types of strings:
BB_sometext
sometext1
sometext 1
I want to differentiate between these three types of strings. Right now, I did this using the following code:
df['NewCol'] = pd.Series()
for i in range(0, len(df)):
if str(df.loc[i, 'OldCol']).split('_')[0] == "BB":
df.loc[i, 'NewCol'] = "A"
elif len(str(df.loc[i, 'OldCol']).split(' ')) == 1:
df.loc[i, 'NewCol'] = "B"
else:
df.loc[i, 'NewCol'] = "C"
Even though this code seems to work, I'm sure there is a better way to do something like this, as this seems very inefficient. Does anyone know a better way to do this? Thanks in advance.

In general, you need something like the following formulation:
>>> df.loc[boolean_test, 'NewCol'] = desired_result
Or, for multiple conditions (Note the parentheses around each condition, and the rather unpythonic & instead of and):
>>> df.loc[(boolean_test1) & (boolean_test2), 'NewCol'] = desired_result
Example
Let's start with an example Data.Frame:
>>> df = pd.DataFrame(dict(OldCol=['sometext1', 'sometext 1', 'BB_ccc', 'sometext1']))
Then you'd do:
>>> df.loc[df['OldCol'].str.split('_').str[0] == 'BB', 'NewCol'] = "A"
To set all BB_ columns to A. You could even (optionally, for readability) separate out the boolean condition onto its own line:
>>> oldcol_starts_BB = df['OldCol'].str.split('_').str[0] == 'BB'
>>> df.loc[oldcol_starts_BB, 'NewCol'] = "A"
I like this method become it means the reader doesn't have to work out the logic hidden within the split('_').str[0] part.
Then, to set all columns with no space, which are still not set (i.e. where isnull is true):
>>> oldcol_has_no_space = df['OldCol'].str.find(' ') < 0
>>> newcol_is_null = df['NewCol'].isnull()
>>> df.loc[(oldcol_has_no_space) & (newcol_is_null), 'NewCol'] = 'C'
Then finally, set all remaining values of NewCol to B:
>>> df.loc[df['NewCol'].isnull(), 'NewCol'] = 'B'
>>> df
OldCol NewCol
0 sometext1 C
1 sometext 1 B
2 BB_ccc A
3 sometext1 C

Related

Python find and replace tool using pandas and a dictionary

Having issues with building a find and replace tool in python. Goal is to search a column in an excel file for a string and swap out every letter of the string based on the key value pair of the dictionary, then write the entire new string back to the same cell. So "ABC" should convert to "BCD". I have to find and replace any occurrence of individual characters.
The below code runs without debugging, but newvalue never creates and I don't know why. No issues writing data to the cell if newvalue gets created.
input: df = pd.DataFrame({'Code1': ['ABC1', 'B5CD', 'C3DE']})
expected output: df = pd.DataFrame({'Code1': ['BCD1', 'C5DE', 'D3EF']})
mycolumns = ["Col1", "Col2"]
mydictionary = {'A': 'B', 'B': 'C', 'C': 'D'}
for x in mycolumns:
# 1. If the mycolumn value exists in the headerlist of the file
if x in headerlist:
# 2. Get column coordinate
col = df.columns.get_loc(x) + 1
# 3. iterate through the rows underneath that header
for ind in df.index:
# 4. log the row coordinate
rangerow = ind + 2
# 5. get the original value of that coordinate
oldval = df[x][ind]
for count, y in enumerate(oldval):
# 6. generate replacement value
newval = df.replace({y: mydictionary}, inplace=True, regex=True, value=None)
print("old: " + str(oldval) + " new: " + str(newval))
# 7. update the cell
ws.cell(row=rangerow, column=col).value = newval
else:
print("not in the string")
else:
# print(df)
print("column doesn't exist in workbook, moving on")
else:
print("done")
wb.save(filepath)
wb.close()
I know there's something going on with enumerate and I'm probably not stitching the string back together after I do replacements? Or maybe a dictionary is the wrong solution to what I am trying to do, the key:value pair is what led me to use it. I have a little programming background but ery little with python. Appreciate any help.
newvalue never creates and I don't know why.
DataFrame.replace with inplace=True will return None.
>>> df = pd.DataFrame({'Code1': ['ABC1', 'B5CD', 'C3DE']})
>>> df = df.replace('ABC1','999')
>>> df
Code1
0 999
1 B5CD
2 C3DE
>>> q = df.replace('999','zzz', inplace=True)
>>> print(q)
None
>>> df
Code1
0 zzz
1 B5CD
2 C3DE
>>>
An alternative could b to use str.translate on the column (using its str attribute) to encode the entire Series
>>> df = pd.DataFrame({'Code1': ['ABC1', 'B5CD', 'C3DE']})
>>> mydictionary = {'A': 'B', 'B': 'C', 'C': 'D'}
>>> table = str.maketrans('ABC','BCD')
>>> df
Code1
0 ABC1
1 B5CD
2 C3DE
>>> df.Code1.str.translate(table)
0 BCD1
1 C5DD
2 D3DE
Name: Code1, dtype: object
>>>

how to apply on certain condition and append value inside it separated by - in pandas through python

I have created a dataframe which contains the columns Name and Mains.
data = [['Anshu', '8321-1328-11'], ['Hero', '83211-1128-11'], ['Naman', '65432-8765-4']]
df = pd.DataFrame(data, columns = ['Name', 'Mains'])
I want to update the Mains column into a new column with an updated value i.e. df['new_mains'] with the following condition: if the number is seprated by 4-4-2, then should be added with 0 and updated separated number will be 5-4-2? is it possible to do so in pandas?
Pretty sure it could be done. For example,
def my_func(strn):
a,b,c = strn.split('-')
new_a = '0'+a if len(a)==4 else a
new_b = '0'+b if len(b)==3 else b
new_c = '9'+c if len(c)==1 else c
return '-'.join([new_a,new_b,new_c])
And then,
df['New_Mains'] = df['Mains'].apply(my_func)
Note: Going off the assumption 'a' is either of length 4 or 5. If 'a', 'b', 'c' are of any other length then, you can also do something like (works for current scenario as well)
new_a = '0'*5-len(a) + a
new_b = '0'*4-len(b) + b
new_c = '9'*2-len(c) + c
More on str.split here. Basically, in your case the string reads something like "99999-9999-99" with "-" as a separator. So,
"99999-9999-99".split('-') #would return
['99999', '9999', '99']
where a = '99999', b = '9999', c = '99'.
new_a, new_b, new_c are variables to hold new values of a, b and c after checking for the conditional statements. Finally join the strings new_a, new_b, new_c to look like original strings from 'Mains' column. More on str.join

how to drop row base on lambda

I have dataframe in which I have to drop row if some of values.
for instance,
x not in ['N/A', ''] where x is columns
is there a way like, apply?
df[x] = df[x].apply(lambda x: x.lower())
I am think in something like:
df.drop.apply(lambda x: X not in ['N/A', ''])???
My DF
F T l
0 0 "0" "0"
1 1 "" "1"
2 2 "2" ""
drop row if T == "" or l == ""
F T l
0 0 "0" "0"
I could not use
df.drop(df.T == "") since the condition ("") depend on runtime data
If you are looking to remove any row that has 'N/A' or '' in the row then you can us a boolean index, just take the inverse of isin() e.g.:
In []:
df[~df.isin(['N/A', '']).any(axis=1)]
Out[]:
F T l
0 0 0 0
If you need to limit to just columns 'A', 'l' then select them, e.g.:
df[~df[['A', 'l']].isin(['N/A', '']).any(axis=1)]
You could also use a dict with isin() but that would only be useful if you had different values for the columns, e.g.:
df[~df.isin({'A': ['N/A', ''], 'l': ['']}).any(axis=1)]
From the following answer, the solution is:
mask = df.pipe(lambda x: (x['T'].isin(['N/A', ''])) | (x['T'].isna()),)
df.drop(df[mask].index, inplace=True)
this allow to provide different lambdas

Filtering dataframes in pandas : use a list of conditions

I have a pandas dataframe with two dimensions : 'col1' and 'col2'
I can filter certain values of those two columns using :
df[ (df["col1"]=='foo') & (df["col2"]=='bar')]
Is there any way I can filter both columns at once ?
I tried naively to use the restriction of the dataframes to two columns, but my best guesses for the second part of the equality don't work :
df[df[["col1","col2"]]==['foo','bar']]
yields me this error
ValueError: Invalid broadcasting comparison [['foo', 'bar']] with block values
I need to do this because the names of the columns, but also the number of columns on which the condition will be set will vary
To the best of my knowledge, there is no way in Pandas for you to do what you want. However, although the following solution may not me the most pretty, you can zip a set of parallel lists as follows:
cols = ['col1', 'col2']
conditions = ['foo', 'bar']
df[eval(" & ".join(["(df['{0}'] == '{1}')".format(col, cond)
for col, cond in zip(cols, conditions)]))]
The string join results in the following:
>>> " & ".join(["(df['{0}'] == '{1}')".format(col, cond)
for col, cond in zip(cols, conditions)])
"(df['col1'] == 'foo') & (df['col2'] == 'bar')"
Which you then use eval to evaluate, effectively:
df[eval("(df['col1'] == 'foo') & (df['col2'] == 'bar')")]
For example:
df = pd.DataFrame({'col1': ['foo', 'bar, 'baz'], 'col2': ['bar', 'spam', 'ham']})
>>> df
col1 col2
0 foo bar
1 bar spam
2 baz ham
>>> df[eval(" & ".join(["(df['{0}'] == {1})".format(col, repr(cond))
for col, cond in zip(cols, conditions)]))]
col1 col2
0 foo bar
I would like to point out an alternative for the accepted answer as eval is not necessary for solving this problem.
from functools import reduce
df = pd.DataFrame({'col1': ['foo', 'bar', 'baz'], 'col2': ['bar', 'spam', 'ham']})
cols = ['col1', 'col2']
values = ['foo', 'bar']
conditions = zip(cols, values)
def apply_conditions(df, conditions):
assert len(conditions) > 0
comps = [df[c] == v for c, v in conditions]
result = comps[0]
for comp in comps[1:]:
result &= comp
return result
def apply_conditions(df, conditions):
assert len(conditions) > 0
comps = [df[c] == v for c, v in conditions]
return reduce(lambda c1, c2: c1 & c2, comps[1:], comps[0])
df[apply_conditions(df, conditions)]
I know I'm late to the party on this one, but if you know that all of your values will use the same sign, then you could use functools.reduce. I have a CSV with something like 64 columns, and I have no desire whatsoever to copy and paste them. This is how I resolved:
from functools import reduce
players = pd.read_csv('players.csv')
# I only want players who have any of the outfield stats over 0.
# That means they have to be an outfielder.
column_named_outfield = lambda x: x.startswith('outfield')
# If a column name starts with outfield, then it is an outfield stat.
# So only include those columns
outfield_columns = filter(column_named_outfield, players.columns)
# Column must have a positive value
has_positive_value = lambda c:players[c] > 0
# We're looking to create a series of filters, so use "map"
list_of_positive_outfield_columns = map(has_positive_value, outfield_columns)
# Given two DF filters, this returns a third representing the "or" condition.
concat_or = lambda x, y: x | y
# Apply the filters through reduce to create a primary filter
is_outfielder_filter = reduce(concat_or, list_of_positive_outfield_columns)
outfielders = players[is_outfielder_filter]
Posting because I ran into a similar issue and found a solution that gets it done in one line albeit a bit inefficiently
cols, vals = ["col1","col2"],['foo','bar']
pd.concat([df.loc[df[cols[i]] == vals[i]] for i in range(len(cols))], join='inner')
This is effectively an & across the columns. To have an | across the columns you can ommit join='inner' and add a drop_duplicates() at the end

Pandas - Updating value in row based on condition with .apply()

Simplified, I would like to use a function to check a condition on a row and set the value accordingly:
def helper(row):
if row["A"] == "TEST":
row["B"] = "WOW"
else:
row["C"] = "NO_GO"
moddf = df.apply(helper, axis=1)
I can do this using iterrows() but .apply should be MUCH faster to iterate over 1M rows in a df.
You don't need (and shouldn't use) apply:
# toy data
df = pd.DataFrame({'A':['TEST','NO'],
'B' : ['A','B'],
'C' :list('12')})
s = df['A']=='TEST'
df.loc[s,'B'] = 'WOW'
df.loc[~s, 'C'] = 'NO_GO'
Output:
A B C
0 TEST WOW 1
1 NO B NO_GO

Categories