pythondataframe如何替换值_替换python数据帧中的值(replace values in a python dataframe)

替换python数据帧中的值(replace values in a python dataframe)

我试图用'映射'数据帧中的映射替换'映射'数据帧中的值。

for column in df:

mapped[column] = df[column].astype(str)

for i, row in mappings.iterrows():

coln = row['colname']

val = row['value']

map = row['mapping']

print 'match::' + coln + ":"+str(val)+ ":"+str(map)

print mapped[mapped[coln]== val]

mapped[coln].replace(str(val), str(map))

print mapped.head()

尽管存在匹配记录,但“映射”数据框中的值不会被替换。 我怎样才能解决这个问题?

I am trying to replace values in the 'mapped' dataframe with mappings in 'mappings' dataframe.

for column in df:

mapped[column] = df[column].astype(str)

for i, row in mappings.iterrows():

coln = row['colname']

val = row['value']

map = row['mapping']

print 'match::' + coln + ":"+str(val)+ ":"+str(map)

print mapped[mapped[coln]== val]

mapped[coln].replace(str(val), str(map))

print mapped.head()

Although there are matching records, the values in 'mapped' dataframe is not getting replaced. How can I fix this?

原文:https://stackoverflow.com/questions/41487697

2020-07-06 09:07

满意答案

mapped[coln].replace(str(val), str(map))

默认情况下, replace不在适当位置。 传递它inplace=True或重新分配它:

mapped[coln].replace(str(val), str(map), inplace=True)

要么

mapped[coln] = mapped[coln].replace(str(val), str(map))

mapped[coln].replace(str(val), str(map))

replace is not inplace by default. Either pass it inplace=True or reassign it:

mapped[coln].replace(str(val), str(map), inplace=True)

or

mapped[coln] = mapped[coln].replace(str(val), str(map))

2017-01-05

相关问答

df.update(dfr.fillna('NaN'))

df.replace('NaN',np.nan)

Out[501]:

A B C D E F

0 aaa abx fwe dcs NaN gsx

1 bbb daf dxs fsx NaN ewe

2 sfa NaN NaN NaN NaN wes

3 ddd NaN NaN asc NaN NaN

4 web NaN NaN cse NaN...

您需要按set_index创建的Series map : s = df.set_index('qid')['body']

result['q1_body'] = result['qid1'].map(s)

result['q2_body'] = result['qid2'].map(s)

print (result)

qid1 q1_body qid2 q2_body

0 1a sfgaks 2a shdfjk

1 1a sfgaks 3a adjkwf

2 2a ...

我认为你需要replace set_index创建的set_index : print (df1)

Id Id.1 Id.2

0 4954733 3929949 515674

1 2950086 1863885 4269069

2 1241018 3711213 4507609

3 3806276 2035233 4968071

4 4437138 1...

您迭代地附加到DataFrame行的建议不是最佳的。 它会减慢代码。 相反,您可以将输出附加到列表中,然后根据需要重新整形列表,并最终将其转换为pd.DataFrame。 这将比你的建议更快。 例如: import pandas as pd, numpy as np

list1=[] #initialize the list

list1.extend([i]) #where i is the output from your loop

df = pd.DataFrame(np.reshape(li...

尝试下面的代码,而不是将'inf'作为字符串: df.replace(-np.Inf, np.nan)

Try the below, rather than 'inf' as a string: df.replace(-np.Inf, np.nan)

如果需要替换NaN值需要函数combine_first或fillna : df['Clean Company Name'].combine_first(df['Company Name'])

要么: df['Clean Company Name'].fillna(df['Company Name'])

样品: df = pd.DataFrame({'Company Name':['s','d','f'], 'Clean Company Name': [np.nan, 'r', 't']})

p...

您可以使用eq将DataFrame值与max : print (tmp[tmp.eq(tmp.max(axis=1), axis=0)])

mask = (tmp.eq(tmp.max(axis=1), axis=0))

print (mask)

A B

0 False True

1 False True

2 True False

3 False True

4 False True

5 False True

6 True Tru...

mapped[coln].replace(str(val), str(map)) 默认情况下, replace不在适当位置。 传递它inplace=True或重新分配它: mapped[coln].replace(str(val), str(map), inplace=True) 要么 mapped[coln] = mapped[coln].replace(str(val), str(map)) mapped[coln].replace(str(val), str(map)) replace is...

replace默认返回数据的副本,因此您需要通过self-assign覆盖df或传递inplace=True : df[[0]].replace(["x"], ["dummy"], inplace=True)

要么 df[0] = df[[0]].replace(["x"], ["dummy"])

看文档 replace returns a copy of the data by default, so you need to either overwrite the df by self-a...

相关文章

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整

...

python里的字典就像java里的HashMap,以键值对的方式存在并操作,其特点如下:通过键来存取

...

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结

...

pychseg - A Python Chinese Segment Project - Google

...

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重

...

简单的数据类型以及赋值 1)变量不需要声明 Python的变量不需要声明,你可以直接输入:>>>a =

...

python的官网:http://www.python.org/ 有两个版本,就像struts1和st

...

Python的文件类型 Python有三种文件类型,分别是源代码文件、字节码文件和优化代码文件

源代

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值