How to replace text in a string column of a Pandas dataframe?
Use the vectorised str
method replace
:
df['range'] = df['range'].str.replace(',','-')
df
range
0 (2-30)
1 (50-290)
EDIT: so if we look at what you tried and why it didn't work:
df['range'].replace(',','-',inplace=True)
from the docs we see this description:
str or regex: str: string exactly matching to_replace will be replaced
with value
So because the str values do not match, no replacement occurs, compare with the following:
df = pd.DataFrame({'range':['(2,30)',',']})
df['range'].replace(',','-', inplace=True)
df['range']
0 (2,30)
1 -
Name: range, dtype: object
here we get an exact match on the second row and the replacement occurs.
How to modify a Pandas Column filled text
You can perform string operations on Pandas series by appending .str to the series name. Here are some examples:
>>> df = pd.DataFrame([{'A': 'Label1', 'B': '$12.00'},
... {'A': 'Label2', 'B': '$14.00'},
... {'A': 'Label1', 'B': '$9.00'},
... {'A': 'Label2', 'B': '$8.00'}])
>>> df.B.str.replace('$','')
0 12.00
1 14.00
2 9.00
3 8.00
Name: B, dtype: object
>>> df.A.str[-1:]
0 1
1 2
2 1
3 2
Name: A, dtype: object
>>> df.A.str[1:]
0 abel1
1 abel2
2 abel1
3 abel2
Name: A, dtype: object
>>> df.B.str.len()
0 6
1 6
2 5
3 5
Name: B, dtype: int64
Pandas documentation: Working with Text Data
Change a dataframe column from text to numbers in pandas dataframe
Use pandas.Series.apply
with lambda:
ozone_data['pluie'] = ozone_data['pluie'].apply(lambda x: 1 if x == 'Pluie' else 2)
or use numpy.where
df['pluie'] = np.where(df['pluie'] == 'Pluie', 1,2)
There are few more ways to achieve the same result.
How to change the font-size of text in dataframe using pandas
You can set one or more properties for each cell using set_properties().
df = pd.DataFrame({
'date': ('2019-11-29', '2016-11-28'),
'price': (0, 1),
})
df = df.style.set_properties(**{
'background-color': 'grey',
'font-size': '20pt',
})
df.to_excel('test.xlsx', engine='openpyxl')
Also you can use apply()
method to customize specific cells:
def custom_styles(val):
# price column styles
if val.name == 'price':
styles = []
# red prices with 0
for i in val:
styles.append('color: %s' % ('red' if i == 0 else 'black'))
return styles
# other columns will be yellow
return ['background-color: yellow'] * len(val)
df = pd.DataFrame(...)
df = df.style.apply(custom_styles)
df.to_excel('test.xlsx', engine='openpyxl')
Also you can use applymap
method which works elementwise. You can find more examples in docs.
Hope this helps.
edit string text in dataframe variable
This is also less elegant than desired, but should work for multiple knots
output %>%
mutate(is_spline = grepl("^rcs\\(.*?, \\d\\)", term),
n_term = str_count(term, "'") + 1,
pre = ifelse(is_spline, paste0('s', n_term, ' '), ""),
term = paste0(pre, gsub("(^rcs\\(.*?, \\d\\))|(\\'+$)", "", term))) %>%
select(-is_spline, -n_term, -pre)
#> # A tibble: 7 x 5
#> term estimate std.error statistic p.value
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 s1 age -0.00306 0.0219 -0.140 0.889
#> 2 s2 age 0.0154 0.0261 0.592 0.554
#> 3 sex -0.525 0.192 -2.74 0.00620
#> 4 ph.ecog 0.421 0.131 3.22 0.00128
#> 5 s1 meal.cal -0.000416 0.00104 -0.400 0.689
#> 6 s2 meal.cal 0.00118 0.00232 0.509 0.611
#> 7 s3 meal.cal -0.00659 0.0114 -0.577 0.564
How to change text from specific cells in a dataframe?
You can do this as follows:
foo[[6]][[50]] <- "BT"
foo[[6]][[51]] <- "BT"
where foo
is the name of your data frame (your screenshot doesn't show that). Regardless of name values, you can always address individual cells in a data frame with the [[column]][[row]]
syntax.
How to change text in specific column in panda data frame?
You can use .str.replace()
on the WIKIURL
series. Example -
df['WIKIURL'] = df['WIKIURL'].str.replace('/wiki/','')
Demo -
In [95]: df
Out[95]:
WIKIURL NUMBER
0 /wiki/blabla 9
1 /wiki/bladiebla 8
2 /wiki/blablabla 1
3 /wiki/kipapapap 2
4 /wiki/wqeqrwtyee 3
5 /wiki/soduyfhlas 1
In [96]: df['WIKIURL'] = df['WIKIURL'].str.replace('/wiki/','')
In [97]: df
Out[97]:
WIKIURL NUMBER
0 blabla 9
1 bladiebla 8
2 blablabla 1
3 kipapapap 2
4 wqeqrwtyee 3
5 soduyfhlas 1
Related Topics
Collapse Consecutive Runs of Numbers to a String of Ranges
How to Programmatically Darken the Color Given Rgb Values
Can .Sd Be Viewed from a Browser Within [.Data.Table()
R: Selecting Subset Without Copying
Drawing Simple Mediation Diagram in R
R - Waiting for Page to Load in Rselenium with Phantomjs
How to Speed Up R Packages Installation in Docker
How to Assign Your Color Scale on Raw Data in Heatmap.2()
Frequency Table with Several Variables in R
Remove a Character from the Entire Data Frame
Grid.Arrange Using List of Plots
Error Installing Packages from Github
How to Increase the Resolution of My Plot in R
Transfer Values from One Dataframe to Another
Plotting Average of Multiple Variables in Time-Series Using Ggplot