Python 如果行值重复,请保留正确的值

Python 如果行值重复,请保留正确的值,python,pandas,Python,Pandas,我有一个与此类似的数据帧 import pandas as pd df = pd.DataFrame({'a': ['A', 'A'], 'b': ['B', 'D'], 'c': ['C', 'C'], 'd': ['D', 'D']}, index=[0, 1]) 如果行具有相同的值,我希望保留正确的单元格 期望输出 0 1 0

我有一个与此类似的数据帧

import pandas as pd
df = pd.DataFrame({'a': ['A', 'A'],
                   'b': ['B', 'D'],
                   'c': ['C', 'C'],
                   'd': ['D', 'D']},
                   index=[0, 1])
如果行具有相同的值,我希望保留正确的单元格

期望输出

  0  1
0    A
1 B  D
2    C
3    D
我已经尝试了
df=pd.DataFrame(列表(map(pd.unique,df.values)))
但它会将值推到最左边,这不是我想要的输出


感谢您的帮助

这应该对您有用:

import pandas as pd
df = pd.DataFrame({'a': ['A', 'A'],
                   'b': ['B', 'D'],
                   'c': ['C', 'C'],
                   'd': ['D', 'D']},
                   index=[0, 1])

df=df.T #Transposing the dataframe

def SameValue(row):
    if row[0] == row[1]:
        return ''        #If the rows match return an empty string
    else:
        return row[0]    #If the rows do not match return the original value


df[0] = df.apply(SameValue, axis=1)   #Apply the SameValue function on the dataframe

print(df)

请让我知道它是否有效

这应该适合您:

import pandas as pd
df = pd.DataFrame({'a': ['A', 'A'],
                   'b': ['B', 'D'],
                   'c': ['C', 'C'],
                   'd': ['D', 'D']},
                   index=[0, 1])

df=df.T #Transposing the dataframe

def SameValue(row):
    if row[0] == row[1]:
        return ''        #If the rows match return an empty string
    else:
        return row[0]    #If the rows do not match return the original value


df[0] = df.apply(SameValue, axis=1)   #Apply the SameValue function on the dataframe

print(df)

请让我知道它是否有效

最简单的方法是:

df = df.T # Transpose of the datafame
df.loc[df[0]==df[1],0] = '' # Find where column 0 and 1 are equal and change the value of column 0 to empty string

最简单的方法是:

df = df.T # Transpose of the datafame
df.loc[df[0]==df[1],0] = '' # Find where column 0 and 1 are equal and change the value of column 0 to empty string

使用pythonic方式而不是数据帧:


df = pd.DataFrame({'a': ['A', 'A'],
                   'b': ['B', 'D'],
                   'c': ['C', 'C'],
                   'd': ['D', 'D']},
                   index=[0, 1])
 

data_in_dict = df.to_dict()
for key in data_in_dict:
   if data_in_dict[key][0] == data_in_dict[key][1]
      data_in_dict[key][0] = ''

final_data_frame = pd.DataFrame(data_in_dict, index=[0,1])
print(final_data_frame.T)

使用pythonic方式而不是数据帧:


df = pd.DataFrame({'a': ['A', 'A'],
                   'b': ['B', 'D'],
                   'c': ['C', 'C'],
                   'd': ['D', 'D']},
                   index=[0, 1])
 

data_in_dict = df.to_dict()
for key in data_in_dict:
   if data_in_dict[key][0] == data_in_dict[key][1]
      data_in_dict[key][0] = ''

final_data_frame = pd.DataFrame(data_in_dict, index=[0,1])
print(final_data_frame.T)