当cluster_name
包含“演示”时,我想将其更改为“未知”。
这是我所管理的最好的事情
df["cluster_name"] = "unknown" if "demo" is in df["cluster_name"] else df["cluster_name"]
但是得到:
SyntaxError:语法无效
答案 0 :(得分:2)
import numpy as np
df["cluster_name"] = np.where(df["cluster_name"].str.contains("demo"), "unknown", df["cluster_name"])
请参见以下示例:
In [814]: df1
Out[814]:
State Year Incident new nn
0 a 1980 513 1 0.0
1 demo is in 1981 453 0 1.0
2 b 1982 424 1 100.0
3 my demo 1983 372 100 NaN
In [816]: df1.State = np.where(df1.State.str.contains('demo'), 'unknown', df1.State)
In [817]: df1
Out[817]:
State Year Incident new nn
0 a 1980 513 1 0.0
1 unknown 1981 453 0 1.0
2 b 1982 424 1 100.0
3 unknown 1983 372 100 NaN
答案 1 :(得分:2)
如果不需要搜索子字符串“ demo”,则可以使用Series.replace
。 “包含”是不明确的。
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Add this line
SizeConfig().init(context);
return SafeArea(...);
}
}
或替换df['cluster_name'] = df['cluster_name'].replace('demo','unknown')
inplace
答案 2 :(得分:0)
一种可能的解决方案是将map
与lambda
函数一起使用,该函数在语法上类似于您尝试执行的操作:
简单的map
解决方案:
#replaces the row with 'unknown' if it is 'demo'
df['cluster_name'] = df['cluster_name'].map(lambda x : 'unknown' if x=='demo' else x)
更广义的map
解决方案:
#replaces the row with 'unknown' if contains 'demo'
df['cluster_name'] = df['cluster_name'].map(lambda x : 'unknown' if 'demo' in x else x)
示例:
>>> #simple map solution
>>> df
cluster_name
0 demo
1 demo
2 1
>>> df['cluster_name'] = df['cluster_name'].map(lambda x : 'unknown' if x=='demo' else x)
>>> df
cluster_name
0 unknown
1 unknown
2 1
>>> #More generalized map solution:
>>> df1
cluster_name
0 demo is a
1 demo
2 1
>>> df1['cluster_name'] = df1['cluster_name'].map(lambda x : 'unknown' if 'demo' in x else x)
>>> df1
cluster_name
0 unknown
1 unknown
2 1