Python:在一个步骤中复制panda并替换特定的列值

2024-09-30 04:39:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何创建一个新的数据帧并用一条语句替换特定列中的值?你知道吗

假设我有以下几点:

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

Tags: nanextrastudentmetouchdf2gpadon
2条回答

使用^{}的嵌套字典语法

df2 = df.replace({'gpa':{'junk':'NaN'}})

从文档中:

Nested dictionaries, e.g., {‘a’: {‘b’: nan}}, are read as follows: look in column ‘a’ for the value ‘b’ and replace it with nan.

请注意,使用'NaN'将用字符串替换它。如果希望它是实际的NaN,请使用np.nan

您可以使用assign创建一个副本并进行替换。你知道吗

df2 = df.assign(gpa = df.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

相关问题 更多 >

    热门问题