【发布时间】:2020-04-19 11:47:58
【问题描述】:
我有两个数据框,如下所示
df_input df_output
id POLL_X POLL_Y POLL_Z .. id Pass_01 Pass_02 Pass_03 .....
110101 1 2 4 110101
110102 2 1 3 110102
要求是根据df_input中的值填写df_ouput
df_input df_output
id POLL_X POLL_Y POLL_Z .... id Pass_01 Pass_02 Pass_03 .....
110101 1 2 3 110101 X Y Z
110102 2 1 3 110102 Y X Z
所以基本上来自 df_input 的列值将是 df_output 中的单元格值,同时匹配并基于 df_input.id == df_output.id
我正在尝试如下
def function1(df_input, number):
dfwithCols = df_input[df_input.columns[pd.Series(df_input.columns).str.startswith('POLL_')]]
list_cols = dfwithCols .columns[(dfwithCols == float(number)).iloc[0]]
colValue = (dfReduced == float(index)).idxmax(axis=1)[0]
return colValue
--驱动函数--
for i in range(1,number_of_columnswithPass):
df_output['Pass_'+i] = function1(df_input,i)
number_of_columnsiwthPass 是一个常数,它给出了名称为 pass 的总列数。
我也不能遍历每一行,因为这将花费大量时间,必须在基于列或基于 lambda 的情况下进行
两个数据框中还有其他列,df_input.id == df_output.id 也必须匹配
总列数可以在 40 左右,一些测试值包括 POLL_DNW , POLL_DO, POLL_DOES , POLL_SIG:2
所以我必须在 '_' 和 01,02,03,04----10,11,--21,---39,40 之类的列号之后取任何内容
【问题讨论】:
-
假设 df_output 中的初始数据为空值是否安全?
-
是的,它是。 null 或 nan
-
但是它的 id 列被填满了
标签: python python-3.x pandas python-2.7 dataframe