replace

Replace None with NaN in pandas dataframe

人盡茶涼 提交于 2020-08-20 17:52:02
问题 I have table x : website 0 http://www.google.com/ 1 http://www.yahoo.com 2 None I want to replace python None with pandas NaN. I tried: x.replace(to_replace=None, value=np.nan) But I got: TypeError: 'regex' must be a string or a compiled regular expression or a list or dict of strings or regular expressions, you passed a 'bool' How should I go about it? 回答1: You can use DataFrame.fillna or Series.fillna which will replace the Python object None , not the string 'None' . import pandas as pd

Replace None with NaN in pandas dataframe

心已入冬 提交于 2020-08-20 17:49:11
问题 I have table x : website 0 http://www.google.com/ 1 http://www.yahoo.com 2 None I want to replace python None with pandas NaN. I tried: x.replace(to_replace=None, value=np.nan) But I got: TypeError: 'regex' must be a string or a compiled regular expression or a list or dict of strings or regular expressions, you passed a 'bool' How should I go about it? 回答1: You can use DataFrame.fillna or Series.fillna which will replace the Python object None , not the string 'None' . import pandas as pd

replace na in a dataframe with value in other df r [duplicate]

荒凉一梦 提交于 2020-08-17 05:56:29
问题 This question already has answers here : Can I replace NAs when joining two data frames with dplyr? (2 answers) Closed 19 days ago . This is an example of my df: x<-tibble::tribble( ~ID, ~Month, ~Value, "A", 1L, 100L, "A", 2L, 200L, "A", 3L, NA, "A", 4L, 400L, "B", 1L, 50L, "B", 2L, 20L, "B", 3L, 30L, "B", 4L, NA, "C", 1L, NA, "C", 2L, 60L, "C", 3L, 70L, "C", 4L, 60L, "D", 1L, 60L, "D", 2L, 60L, "D", 3L, 60L, "D", 4L, 50L ) And I have another df with this values: y<-tibble::tribble( ~Month,

Python: Replace multiple strings in text file with multiple inputs

妖精的绣舞 提交于 2020-08-09 06:30:14
问题 I am looking at replacing multiple strings with input from the user. The following code (which is a modification of a code from one of the queries here in stackoverflow; pardon coz I can't find the thread anymore) works well when finding and replacing one instance(done on purpose) of a specified string: print('What word should we replace s1 with?') input_1 = input() with open('C:\\dummy1.txt')as f: sample1 = f.read().replace("s1", str(input_1), 1) with open('C:\\dummy2.txt',"w") as f1: f1

Python - replace values in dataframe based on another dataframe match

随声附和 提交于 2020-08-05 06:12:48
问题 Suppose that I have 2 Python data frame2 named A and B like shown below. How could I replace column Value in data frame A based on the matches of columns ID and Month from B? Any ideas? Thanks Dataframe A: ID Month City Brand Value 1 1 London Unilever 100 1 2 London Unilever 120 1 3 London Unilever 150 1 4 London Unilever 140 2 1 NY JP Morgan 90 2 2 NY JP Morgan 105 2 3 NY JP Morgan 100 2 4 NY JP Morgan 140 3 1 Paris Loreal 60 3 2 Paris Loreal 75 3 3 Paris Loreal 65 3 4 Paris Loreal 80 4 1

replace with php all alphanummeric chars with X or x (case sensitive)

为君一笑 提交于 2020-07-22 10:43:07
问题 I have the following variables: $var1 = "23 Jan 2014"; $var2 = "Some Text Here - More Text is written here"; How can I replace the text so the output looks like this: $var1 = "XX Xxx XXXX"; $var2 = "Xxxx Xxxx Xxxx - Xxxx Xxxx xx xxxxxx xxxx"; Edit: The variables can change. I simply want to replace all A-Za-z0-9 from any variable with X (for capital letters), x (for small letters) and X (for numbers). 回答1: Use double regular expressions - one for the upper case + numbers and one for the lower

replace with php all alphanummeric chars with X or x (case sensitive)

假如想象 提交于 2020-07-22 10:42:59
问题 I have the following variables: $var1 = "23 Jan 2014"; $var2 = "Some Text Here - More Text is written here"; How can I replace the text so the output looks like this: $var1 = "XX Xxx XXXX"; $var2 = "Xxxx Xxxx Xxxx - Xxxx Xxxx xx xxxxxx xxxx"; Edit: The variables can change. I simply want to replace all A-Za-z0-9 from any variable with X (for capital letters), x (for small letters) and X (for numbers). 回答1: Use double regular expressions - one for the upper case + numbers and one for the lower