Ary*_*rya 5 python numpy dataframe pandas
我们有数据如下
Name value1 Value2 finallist
0 cosmos 10 20 [10,20]
1 network 30 40 [30,40]
2 unab 20 40 [20,40]
Run Code Online (Sandbox Code Playgroud)
有什么办法可以区分所有行
最终输出类似
Name value1 Value2 finallist cosmos network unab
0 cosmos 10 20 [10,20] 0 40 30
1 network 30 40 [30,40] 40 0 10
2 unab 20 40 [20,40] 30 10 0
Run Code Online (Sandbox Code Playgroud)
数据有不同类型的名称,每个名称应该是一列
您需要每行值总和的成对绝对差。最简单的可能是使用底层的 numpy 数组。
# get sum of values per row and convert to numpy array
a = df['value1'].filter(regex='(?i)value').sum(1).to_numpy()
# compute the pairwise difference, create a DataFrame and join
df2 = df.join(pd.DataFrame(abs(a-a[:,None]), columns=df['Name'], index=df.index))
Run Code Online (Sandbox Code Playgroud)
输出:
Name value1 Value2 finallist cosmos network unab
0 cosmos 10 20 [10, 20] 0 40 30
1 network 30 40 [30, 40] 40 0 10
2 unab 20 40 [20, 40] 30 10 0
Run Code Online (Sandbox Code Playgroud)