对于pandas数据帧的特定列我想使元素全部为大写并替换空格
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
我得到了
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)
有什么建议可以避免这种警告或更好的方法来做我想做的事情吗?
答案 0 :(得分:1)
您可以使用mat
选择要修改的行来简化此操作:
numpy.where
答案 1 :(得分:1)
str.upper
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
答案 2 :(得分:0)
您可以用
替换最后一行df.loc[temp2.index, 'A'] = temp2.values