问题
for a specific column of a pandas dataframe I would like to make the elements all uppercase and replace the spaces
import pandas as pd
df = pd.DataFrame(data=[['AA 123',00],[99,10],['bb 12',10]],columns=['A','B'],index=[0,1,2])
# find elements 'A' that are string
temp1 = [isinstance(s, str) for s in df['A'].values]
# Make upper case and replace any space
temp2 = df['A'][temp1].str.upper()
temp2 = temp2.str.replace(r'\s', '')
# replace in dataframe
df['A'].loc[temp2.index] = temp2.values
I get
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py:194: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
self._setitem_with_indexer(indexer, value)
Any suggestion to avoid this warning or any better way to do what I am trying to do?
回答1:
You can simplify this a lot by using numpy.where
to select the rows you want to modify:
import pandas as pd
import numpy as np
df = pd.DataFrame(data=[['AA 123',00],[99,10],['bb 12',10]],columns=['A','B'],index=[0,1,2])
df['A'] = np.where(df['A'].apply(lambda x: isinstance(x, str)),
df['A'].str.upper().str.replace(r'\s', ''),
df['A'])
回答2:
str.upper
with replace
df['A'] = df.A.str.upper().replace('\s+', '', regex=True).fillna(df['A'])
A B
0 AA123 0
1 99 10
2 BB12 10
回答3:
You could replace your last line with
df.loc[temp2.index, 'A'] = temp2.values
来源:https://stackoverflow.com/questions/50414050/make-upper-case-and-replace-space-in-column-dataframe