如何创建一个新的数据帧并用一条语句替换特定列中的值?你知道吗
假设我有以下几点:
import pandas as pd
import numpy as np
student_ids = ['abc123', 'def321', 'qwe098', 'rty135']
extra_junk = ['whoa', 'hey', 'don\'t touch me', 'junk']
gpas = ['3.1', 'junk', 'NaN', '2.75']
aa = np.array([student_ids, extra_junk, gpas]).transpose()
df = pd.DataFrame(data= aa, columns=['student_id', 'extra_junk', 'gpa'])
>>> df
student_id extra_junk gpa
0 abc123 whoa 3.1
1 def321 hey junk
2 qwe098 don't touch me NaN
3 rty135 junk 2.75
我可以用两种方法:
df2 = df.copy()
df2['gpa'] = df2['gpa'].replace('junk', 'NaN')
>>> df2
student_id extra_junk gpa
0 abc123 whoa 3.1
1 def321 hey NaN
2 qwe098 don't touch me NaN
3 rty135 junk 2.75
使用^{} 的嵌套字典语法
从文档中:
请注意,使用
'NaN'
将用字符串替换它。如果希望它是实际的NaN
,请使用np.nan
您可以使用
assign
创建一个副本并进行替换。你知道吗输出:
相关问题 更多 >
编程相关推荐