英文:
How do I replace a string-value in a specific column using method chaining?
问题
我有一个pandas数据框,其中一些字符串值是"NA"。我想使用方法链接来替换特定列(例如下面的'strCol')中的这些值。
我该如何做?(尽管这应该很容易,但我进行了相当多的谷歌搜索,但没有成功!...)
这是一个简单的示例:
```python
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4],
'B':['val1','val2','NA','val3']})
df = (
df
.rename(columns={'A':'intCol', 'B':'strCol'}) # 方法链接示例操作1
.astype({'intCol':float}) # 方法链接示例操作2
# .where(df['strCol']=='NA', pd.NA) # 如何在这里替换字符串'NA'?这种方法不起作用...
)
df
<details>
<summary>英文:</summary>
I have a pandas data frame, where some string values are "NA". I want to replace these values in a specific column (i.e. the 'strCol' in the example below) using method chaining.
How do I do this? (I googled quite a bit without success even though this should be easy?! ...)
Here is a minimal example:
```python
import pandas as pd
df = pd.DataFrame({'A':[1,2,3,4],
'B':['val1','val2','NA','val3']})
df = (
df
.rename(columns={'A':'intCol', 'B':'strCol'}) # method chain example operation 1
.astype({'intCol':float}) # method chain example operation 2
# .where(df['strCol']=='NA', pd.NA) # how to replace the sting 'NA' here? this does not work ...
)
df
答案1
得分: 2
你可以尝试使用replace
而不是where
:
df.replace({'strCol': {'NA': pd.NA}})
英文:
You can try replace instead of where:
df.replace({'strCol':{'NA':pd.NA}})
答案2
得分: 0
使用lambda
在where
子句中来评估链式数据框:
df = (df.rename(columns={'A':'intCol', 'B':'strCol'})
.astype({'intCol':float})
.where(lambda x: x['strCol']=='NA', pd.NA))
输出:
>>> df
intCol strCol
0 NaN <NA>
1 NaN <NA>
2 3.0 NA
3 NaN <NA>
许多方法,如where
、mask
、groupby
、apply
,可以接受一个可调用对象或函数,因此可以传递一个lambda函数。
英文:
Use lambda
in where
clause to evaluate the chained dataframe:
df = (df.rename(columns={'A':'intCol', 'B':'strCol'})
.astype({'intCol':float})
.where(lambda x: x['strCol']=='NA', pd.NA))
Output:
>>> df
intCol strCol
0 NaN <NA>
1 NaN <NA>
2 3.0 NA
3 NaN <NA>
Many methods like where
, mask
, groupby
, apply
can take a callable or a function so you can pass a lambda function.
答案3
得分: 0
> 替换条件为假的值。
所以您需要在要进行替换的地方使条件 不 成立,以下是一个简单的示例
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4,5,6,7,8,9]})
df2 = df.where(df.x%2==0,-1)
print(df2)
得到的输出是
x
0 -1
1 2
2 -1
3 4
4 -1
5 6
6 -1
7 8
8 -1
注意,奇数 值被替换为 -1
,而对于 偶数 值,条件成立。
英文:
> Replace values where the condition is False.
So you need condition to not hold where you want to make replacement, simple example
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4,5,6,7,8,9]})
df2 = df.where(df.x%2==0,-1)
print(df2)
gives output
x
0 -1
1 2
2 -1
3 4
4 -1
5 6
6 -1
7 8
8 -1
Observe that odd values were replaced by -1
s, whilst condition does hold for even values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论