答案 0 :(得分:0)
不幸的是,您将不得不遍历行和列以选择非空单元格。这是表格的简化版本。
import pandas as pd
df =pd.DataFrame({
'em':['em1','em2','em3','em1','em2','em3'],
'a':['a1','a2','a3','','',' '],
'b':['','','','b1','b2','b3']
})
print(df)
df2 =pd.DataFrame()
for em in set(df['em']):
df_em = df[df['em']==em]
df2 = df2.append(df_em.iloc[[0]], ignore_index = True)
for c in range(len(df.columns)):
if df_em.iat[0,c].strip() == '':
df2.iat[len(df2)-1,c] = df_em.iat[1,c]
else:
df2.iat[len(df2)-1,c] = df_em.iat[0,c]
print('\n\n',df2)
输出:
em a b
0 em1 a1
1 em2 a2
2 em3 a3
3 em1 b1
4 em2 b2
5 em3 b3
em a b
0 em1 a1 b1
1 em2 a2 b2
2 em3 a3 b3
答案 1 :(得分:-1)
如果您的Name
和Email
和Subject
列等于匹配行,并且您知道要“合并”的行组的索引,则可以尝试以下步骤:
combine_first
填充来自不同行组的空值。在您的情况下,行组是前三个和后三个。
new_df = df.set_index(["Name", "Email", "Subject"])
# Combine first 3 rows with last 3
combined = new_df.iloc[:3].combine_first(new_df.iloc[3:])
结果:
Q.1 Q.2 Q.3 Q.4 Q.5 Q.6 Q.7 Q.8
Name Email Subject
MOHD AQUIL aquilchoudhary@gmail.com History B C B A B A B D
Shweta Rangarishweta@gmail.co History B A B D B C B A
Suraj surajdas112@gmail.com History B A B B B A B B