制作大写并替换列数据框中的空格 [英] make upper case and replace space in column dataframe

查看:34
本文介绍了制作大写并替换列数据框中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于熊猫数据框的特定列,我想让元素全部大写并替换空格

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

我明白

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?

推荐答案

您可以通过使用 numpy.where 来选择要修改的行,从而大大简化:

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'])

这篇关于制作大写并替换列数据框中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆