python - 添加新列并删除重复项,以逐列替换空值

标签 python pandas

Duplication type:
Check this column only (default)
Check other columns only
Check all columns

Use Last Value:
True - retain the last duplicate value
False - retain the first of the duplicates (default)

此规则应向数据框添加一个新列,其中包含与任何唯一列的源列相同的列,并且对任何重复列为空。

基本代码是 df.loc[df.duplicated(),get_unique_column_name(df, "clean")] = df[get_column_name(df, column)] duplicated() 的参数根据复制类型设置

参见上面此函数的引用:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.duplicated.html

您应该根据duplication_type 的设置来指定subset 参数中的列

你应该根据上面的use_last_value指定use_last_value

这是我的文件。

Jason   Miller  42  4   25
Tina    Ali     36  31  57
Jake    Milner  24  2   62
Jason   Miller  42  4   25
Jake    Milner  24  2   62
Amy     Cooze   73  3   70
Jason   Miller  42  4   25
Jason   Miller  42  4   25
Jake    Milner  24  2   62
Jake    Miller  42  4   25

我想通过在下面的文件中使用 pandas.in 来获得这样的结果,我选择了 2 列。

Jason   Miller  42  4   25
Jake    Ali     36  31  57
Jake    Milner  24  2   62
Jason   Miller      4   25
Jake    Milner      2   62
Jake    Cooze   73  3   70
Jason   Miller      4   25
Jason   Miller      4   25
Jake    Milner      2   62
Jake    Miller      4   25

请任何人回复我的问题。

最佳答案

您可以使用 DF.duplicated并分配 C 列的值,其中第一次出现的值出现在 A 列和 B 列中。

然后您可以用空字符串填充生成的 Nans 以生成所需的数据帧。

df = pd.read_csv(data, delim_whitespace=True, header=None, names=['A','B','C','D','E'])
df.loc[~df.duplicated(), "C'"] = df['C']
df.fillna('', inplace=True)
df = df[["A","B", "C'","D","E"]]
print(df)

       A       B  C'   D   E
0  Jason  Miller  42   4  25
1   Tina     Ali  36  31  57
2   Jake  Milner  24   2  62
3  Jason  Miller       4  25
4   Jake  Milner       2  62
5    Amy   Cooze  73   3  70
6  Jason  Miller       4  25
7  Jason  Miller       4  25
8   Jake  Milner       2  62
9   Jake  Miller  42   4  25

另一种方法是获取重复列的子集,并将相关列替换为空字符串。然后,您可以使用 update使用原始 df 修改数据框。

In [2]: duplicated_cols = df[df.duplicated(subset=['C', 'D', 'E'])]

In [3]: duplicated_cols
Out[3]: 
       A       B   C  D   E
3  Jason  Miller  42  4  25
4   Jake  Milner  24  2  62
6  Jason  Miller  42  4  25
7  Jason  Miller  42  4  25
8   Jake  Milner  24  2  62
9   Jake  Miller  42  4  25

In [4]: duplicated_cols.loc[:,'C'] = ''

In [5]: df.update(duplicated_cols)

In [6]: df
Out[6]: 
       A       B   C     D     E
0  Jason  Miller  42   4.0  25.0
1   Tina     Ali  36  31.0  57.0
2   Jake  Milner  24   2.0  62.0
3  Jason  Miller       4.0  25.0
4   Jake  Milner       2.0  62.0
5    Amy   Cooze  73   3.0  70.0
6  Jason  Miller       4.0  25.0
7  Jason  Miller       4.0  25.0
8   Jake  Milner       2.0  62.0
9   Jake  Miller       4.0  25.0

关于python - 添加新列并删除重复项,以逐列替换空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38997069/

相关文章:

python - ImportError [E048] 无法从 spacy.lang 导入语言 en

python - 无法删除 [未命名 :0] and NaN field data

python - 删除 pandas 数据框中的行

python - pandas系列上的短路numpy逻辑_and

python - Docker - Elasticsearch - 无法建立新连接 : [Errno 111] Connection refused', ))

python - Django 管理员添加表单 ajax 调用

Python pandas 根据时间间隔减少回填,直到达到一定数量

python - Pandas 合并数据框

python - Pandas :从左到右和从右到左交替 iterrows()

python - np.random.permutation, np.random.choice 的时间表现