如何根据列名转换/插入/重新绑定Pandas DataFrames中的列?

时间:2015-07-22 19:35:18

标签: python pandas dataframe interpolation

如何使用插值/重新分组从df_input转换为df_output?

import pandas as pd
df_input=pd.DataFrame([ [1,2],[1,3],[0,-6]], columns=[2,5])
df_output=pd.DataFrame([ [1,0.67,1.33],[1,1,2],[0,-2,-4]], columns=[2, 3, 6])

df_input:

   2  5
0  1  2
1  1  3
2  0 -6

df_output:

   2     3     6
0  1  0.67  1.33
1  1  1.00  2.00
2  0 -2.00 -4.00

例如

从df_input.loc [0,5](值= 2)到df_output.loc [0,3](值= 0.67)和df_output.loc [0,6](值= 1.33),去掉根据列名的值完成:

0.67 =  2 * (6-5) / (6-3)
1.33 =  2 * (5-3) / (6-3)

专栏" 2"在df_input和df_output之间是相同的" 2"是[2,5]和[2,3,6]。

这是一个简单的例子,我也想将列名[2,5]和[2,3,6]推广到任何给定的数字向量。

非常感谢。

0 个答案:

没有答案