All Questions
68 questions
1
vote
2
answers
76
views
Pandas: Conditional partial replacing based on different columns using a dictionary
I'm not familiar with the use of dictionaries in Python in its entirety, but I believe they can come in quite handy for my issue.
Say I have the following dataframe:
df = pd.DataFrame({'NATIONALITY': {...
0
votes
1
answer
33
views
Pandas : retrieve mapping of pair of column values to replace value in 3rd one
I've been wasting a shit ton of time on this so here I go: my dataset from the terrorism data of University of Maryland, has some variables 3 of which are basically: country_name, region_name, ...
0
votes
1
answer
36
views
Mapping values with a condition, Pandas
State City
0 California 1
1 California 2
2 Lousiana 1
3 Virginia 2
dic = {1 : LA, 2 : SF}
I am trying to map the dictionary I have on the table with the condition that ...
2
votes
2
answers
1k
views
Replace column values in python dataframe using dictionary based on condition
I have below dataframe
df = pd.DataFrame({'col1': {0: '4', 1: '4', 2: '2'},'col2': {0: 'USA', 1: 'England', 2: 'Japan'}})
>>> df
col1 col2
0 4 USA
1 4 England
2 2 ...
0
votes
2
answers
41
views
How to replace dataframe column values with dictionary list keys?
Supose I have a dictionary:
dic = {'1' : ['A', 'B', 'C'], '2' : ['D', 'E'] , '3' : ['F']}
and a data frame
df = pd.DataFrame()
df["ID"] = pd.Series(["A","B","C&...
1
vote
5
answers
395
views
Python: replace columns using dictionary
I have a DataFrame that looks like this:
df:
date price region currency
2022-01-01 1 NorthAmerica UnitedStatesDollar
2022-01-01 1.28 NorthAmerica CanadianDollar
2022-01-01 1.42 ...
1
vote
2
answers
2k
views
pandas: replace column value with keys and values in a dictionary of list values
I have a dataframe and a dictionary as follows (but much bigger),
import pandas as pd
df = pd.DataFrame({'text': ['can you open the door?','shall you write the address?']})
dic = {'Should': ['can','...
0
votes
0
answers
36
views
Using a dictionary that looks like {'x':'y'} to look for 'x' in column A, if found, replace it with value 'y' in column B [duplicate]
Here's my df:
data = {'Country':['France', 'Spain', 'Japan', 'South Korea', 'South Africa', 'Egypt'],
'Region':['', 'Europe', '', 'Asia', '', 'Africa']}
df = pd.DataFrame(data)
I also have the ...
0
votes
2
answers
235
views
Map two dataframe base on a column and create a new column. Also match partial matching
I have two dataframe
One with codes and values need to map to other dataframe
B = pd.DataFrame({'Code': ['a', 'b', 'c', 'a', 'e','b','b','c'],
'Value': ["House with indoor pool&...
0
votes
1
answer
340
views
how to replace substring of column values with dictionary keys in pandas
I know I can replace column values with dictionary keys in pandas with
dictionary={'A': 'Y'}
df[col].replace(dictionary, inplace=True)
but what if I have a column value "A;B" and I want to ...
4
votes
1
answer
5k
views
Replace Pandas DataFrame column values based on containing dictionary keys
Here is an example, where column is assigned when the row is a dictionary key: https://stackoverflow.com/a/20250996/12603542
What I am looking for is case, where column is assigned when the row ...
1
vote
1
answer
386
views
pandas: replace with a dictionary does not work with string of sentences
I have a dataframe as follows:
import pandas as pd
df = pd.DataFrame({'text':['Lary Page is visiting today',' His boss, Maria Jackson is here.']})
I have extracted the names in the list below. and ...
-2
votes
1
answer
266
views
DataFrame replace multiple keys in a column
Given 2 DataFrames :
DF1 (a Serie) :
Col_Name_1
[['A, B']
['B']
['A, C']
['B, C']]
DF2 :
Col_Name_X, Col_Name_Y
[['A', 'Paris']
['B', 'London']
['C', 'Mexico']]
In DF1, I need to replace all values ...
0
votes
3
answers
775
views
Iterate over df and replace values with a dict
I have a similar situation as this example.
I have a pandas dataframe with 5 rows and 5 columns.
I have in this df only 0 and 1:
And i have a dict that tell me that for example that the value in the ...
0
votes
0
answers
102
views
map replace between two dataframes python
I need to replace values of a column in a dataframe with values from another dataframe based on if condition.
Here is sample example of my question:
Let's say,I have two data frames like below.
first -...
3
votes
1
answer
2k
views
Replace values in columns of dataframe based on dictionary not working [duplicate]
You can read the exact problem below, but this is essentially what I'm trying to do:
df1 = pd.DataFrame({'A':['A0', 'A1', 'A2', 'A3'],
'B': ['B0', 'B1', 'B2', 'B3'],
...
1
vote
2
answers
751
views
how to map and concat value two data frame python
one dataframe as
first = pd.DataFrame({'Code': [1,2,3,4],'Value': ['Apple','Ornage','Bannana', 'Graps']})
and another data frame is
second= pd.DataFrame({'Code': ['1','2','1','2,4','3'],'Product id': ...
1
vote
1
answer
216
views
Replace values of a DataFrame with values of a dictionary
I have two DataFrames with many thousands of rows. The columns all have the dtype string. A snippet looks like this:
DF1:
ID SUCCESSOR
0 0001234 3620031
1 ...
1
vote
1
answer
57
views
Dictionary Unique Keys Rename and Replace
I have a dictionary format structure like this
df = pd.DataFrame({'ID' : ['A', 'B', 'C'],
'CODES' : [{"1407273790":5,"1801032636":20,"1174813554":1,"1215470448":...
1
vote
2
answers
213
views
Pandas replace with dictionary doesn't work for CSV file
I want to replace abbreviations with full words in a single column in a csv file. The csv file has 2 columns (separated by a pipe symbol) with thousands of rows, no headers, looking like this:
...
2
votes
2
answers
67
views
python3 - apply a regex map to column
How to apply a regex to a data frame column?
import pandas as pd
df = pd.DataFrame({'col1': ['negative', 'positive', 'neutral', 'neutral', 'positive']})
cdict = {'n.*': -1, 'p.*': 0}
df['col2'] = df['...
0
votes
1
answer
51
views
Can't retrieve data from Pandas DF, can't replace values in column in Pandas DF
I'm working with adult.data.csv https://archive.ics.uci.edu/ml/datasets/Adult
I wrote this code:
data = pd.read_csv('adult.data.csv', sep = ";")
data[data['salary'] == '>50K']['marital-...
0
votes
2
answers
44
views
How to replace pd.Series list values with dictionary values
After stemming words with NLTK, got dictionary:
> {'golden': 'gold', 'wonderfully': 'wonder', 'damaging': 'damag',
> 'useless': 'use', 'toys': 'toy', 'ducks': 'duck'}
So I want replace ...
1
vote
1
answer
2k
views
Python replace error: replace() argument 2 must be str, not Series
I want to update a string with html code. The string is something like this:
textHTMLextract = 'Hello, my name is name1 name2'
And I have a dataframe with variables like this:
import pandas as pd
...
1
vote
1
answer
322
views
compare two columns and keep difference between strings
Before upgrading pandas from 0.25.1 to 1.1.3 , I was able to strip strings in a dataframe from this:
+---+------------------+------------------+
| | strip_me | pattern |
+---+-------...
1
vote
1
answer
122
views
How to replace data in pandas by using values in dict?
I have a series which contains several numbers. I want to replace them to other string type data by using dictionary values. But I don't know how to do that...
GDP_group['GdpForYearPer$1M'].head(5)
0 ...
0
votes
1
answer
28
views
how to replace list of columns with condition on one col by dict\df
i like to change my the values in list of columns by condition on one col use dict\df
i got df\dict the
Something like this
list_of_cols=["col1",.."col5"]
df[list_of_cols]=where[df[...
0
votes
1
answer
72
views
map column according to nested dictionary accorging to second column
Using vectorization, I want to remap a column according to a nested dictionary depending on the value of a second column. In iteration logic, I would go through the rows; depending on the value in the ...
1
vote
1
answer
39
views
Using a dictionary to replace strings not working
I am trying to use the following code to make replacements in a pandas dataframe however:
replacerscompanya = {',':'','.':'','-':'','ltd':'limited','&':'and'}
df1['CompanyA'] = df1['CompanyA']....
1
vote
2
answers
158
views
How to replace value in whole dataframe based on dictionary when part of string match?
Hi I have 2 dataframes where i have to use 1 dataframe to replace the value in other. I can normally create the dictionary to replace values in whole dataframe but I have a bit different value in ...
0
votes
1
answer
698
views
Issues replacing text in Pandas DataFrame where apostrophes appear
I'm using a Pandas DataFrame, which I have read in from Excel I and want to find and replace contractions in text (e.g. don't -> do not).
The code I'm using works when replacing text which doesn't ...
-1
votes
2
answers
1k
views
How to merge two dictionaries and replace values based on key?
dict1 = [{"names": 'john, will', "age": 2, "score": 4.1}{"names": 'tom, sam', "age": 3, "score": 6}]
dict2 = {'john': '001','will': '002', 'tom': '003', 'sam': '004}
dict1 is a nested dictionary in a ...
3
votes
1
answer
104
views
Replace strings using a dictionary without deleting characters in a pandas dataframe
I have a kind of lookup problem where I have tried to use functions replace dict zip (see below) but that does not exactly produce my desired result because characters (underscores) are removed in the ...
2
votes
3
answers
2k
views
Replace specific column values in pandas dataframe [duplicate]
df = pd.DataFrame({'Tissues':['a1','x2','y3','b','c1','v2','w3'], 'M':[1,2,'a',4,'b','a',7]})
df.set_index('Tissues')
The dataframe looks like:
M
Tissues
a1 1
x2 2
y3 a
...
1
vote
1
answer
42
views
alteration of use values in dictionary to replace values in column
This question is a followup to use values in dictionary to replace values in column. The following dataframe is a modication of the
dataframe and dictionary used in use values in dictionary to ...
2
votes
2
answers
196
views
use values in dictionary to replace values in column
import pandas as pd
df= pd.DataFrame({'Data':['Hey this is 123456 Jonny B Good',
'This is Jonny B Good at 511-233-1137',
'Wow that is ...
1
vote
1
answer
48
views
Replacing values in a df with values from another df
I have a data table df1 that looks like this (result of a df.groupby('id').agg(lambda x: x.tolist())):
df1:
id people
51 [125, 126, 127, 128, 129]
52 [302, 303, 128]
53 [312]
In ...
1
vote
1
answer
47
views
Number formatting after mapping?
I have a data frame with a number column, such as:
CompteNum
100
200
300
400
500
and a file with the mapping of all these numbers to other numbers, that I import to python and convert into a ...
0
votes
0
answers
194
views
Pandas replace from dictionary and regex not working
I am noticing a strance bahavior in the pandas replace function when trying to pass a dictionary with dictionaries as so:
mutual_format = {'col1' : {'(?i)o':'0', '[^\d)]':''},
'col2'...
3
votes
4
answers
525
views
How to replace group of strings in pandas series based on a dictionary with values as list?
I couldnt find a solution in stackoverflow for replacing based on dictionary where the values are in a list.
Dictionary
dct = {"LOL": ["laught out loud", "laught-out loud"],
"TLDR": ["too ...
0
votes
1
answer
2k
views
Replace pandas column values using a key on another column and value from a dictionary
I have searched stackoverflow and gone over a few questions which are related to this, i.e
Remap values in pandas column with a dict
The key distinction is these questions answer how to replace ...
0
votes
1
answer
331
views
Text substitution using Python
I have two datasets. One contains text description and another that contains a table with two columns from_value and to_value.
The idea is to replace all occurrences of the word in the text ...
0
votes
0
answers
70
views
Pandas: change column based on dataframe [duplicate]
I am trying to fix my df as it has some wrong values (that are not NA/NAN, just wrong), with the values from another dataframe (df1).
I already know which ones are wrong and I got the all sorted in ...
7
votes
1
answer
5k
views
Replace words in pandas Dataframe using dictionary
I have a pandas dataframe
id text
1 acclrtr actn corr cr
2 plate corr aff
3 alrm alt
and dictionary
dict={'acclrtr':'accelerator','actn':'action','corr':'corrosion','cr':'chemical resistant'...
1
vote
2
answers
217
views
How to replace a number with another number using a dictionary of replacements in python?
I have two columns of numbers: one column a record of student SAT scores, and another, their corresponding scores on the ACT test in a python pandas dataframe.
SAT Score ACT Score
0 2160....
0
votes
1
answer
595
views
Replace values from pandas dataset with dictionary
I am extracting a column from excel document with pandas. After that, I want to replace for each row of the selected column, all keys contained in multiple dictionaries grouped in a list.
import ...
1
vote
1
answer
193
views
pandas - pd.replace and TypeError
I have all_data dataframe. I want to replace some categorical values in certain columns with numerical values. I'm trying to use this nested dictionary notation (I've checked that the brackets and ...
-1
votes
1
answer
66
views
Replace is producing weird answers in pandas python
i am using dictionary key value pair to replace some string.
dict= {'MAA':'MADRAS', 'MAD':'MADRID'}
now using .replace(), it replaces MAA to MADRAS but MAD of MADRAS is again replaced by MARDRID. ...
30
votes
2
answers
33k
views
Use dictionary to replace a string within a string in Pandas columns
I am trying to use a dictionary key to replace strings in a pandas column with its values. However, each column contains sentences. Therefore, I must first tokenize the sentences and detect whether a ...