I am attempting to read through keys in a dictionary and if these keys are in the values of column A then I want column B to be filled with the values of the dictionary that match that key/keys.
For example;
Column A | Column B |
---|---|
KSTRSHRASA | NaN |
someWord-hi | NaN |
Dictionary:
dict = {'ks': 'Killos',
'RAS': 'Round Point System',
'hi': 'Hello World',
'vc': 'VVCCR'}
Resulting in the below dataframe:
Column A | Column B |
---|---|
KSTRSHRASA | ['killos', 'Round Point System'] |
someWord-hi | ['Hello World'] |
I have tried to do the following:
for k,v in dict.items():
if k.lower() in str(dataframe['Column A']).lower():
v
dataframe['Column B'] = [v]
but it results in the below error: ValueError: Length of values (1) does not match length
of index (6483)
I wrote a code that can perform this if the input is one text but I am unable to apply it to the entire column. I have also tried to convert the dictionary to a dataframe and do it that way but still no luck.
The code I wrote for a singular text input:
text = str(input('Please insert hostname here: '))
results = list()
for k,v in codes.items():
if k.lower() in text.lower():
v
results.append(v)
else:
continue
#print(v)
print ('The type of this device is most likely a/an: ',results)
so the question is:
Is there a way to have python read through the keys in the dictionary and return its corresponding values in Columb B of a dataframe if those keys are contained (exist in) the items in Column A?
CodePudding user response:
This is what I currently have.
dataframe = {'Column A': ['KSTRSHRASA', 'someWord-hi'], 'Column B': [np.nan, np.nan]}
df = pd.DataFrame(dataframe)
dictionary = {'ks': 'Killos',
'RAS': 'Round Point System',
'hi': 'Hello World',
'vc': 'VVCCR'}
# convert dictionary key to lower case
dictionary = {k.lower(): v for k, v in dictionary.items()}
# keep a copy of original dataframe
df_original = df.copy()
# convert dataframe column A to lower case
df['Column A'] = df['Column A'].str.lower()
# if dataframe column A contains dictionary key, append it to column B as a list
df['Column B'] = df['Column A'].apply(lambda x: [dictionary[i] for i in dictionary if i in x])
# convert Column A back to original case
df['Column A'] = df_original['Column A']
OUTPUT:
Column A Column B
0 KSTRSHRASA [Killos, Round Point System]
1 someWord-hi [Hello World]
Hope this helps!