简体   繁体   English

用数组替换pandas数据帧的一部分

[英]Replace part of a row of a pandas dataframe with an array

I have a dataframe df1 that looks like this: 我有一个如下所示的数据帧df1

     Sample_names    esv0     esv1   esv2   ...    esv918  esv919  esv920  esv921
0    pr1gluc8NH1     2.1      3.5   6222   ...         0       0       0       0
1    pr1gluc8NH2  3189.0     75.0   9045   ...         0       0       0       0
2  pr1gluc8NHCR1     0.0   2152.0  12217   ...         0       0       0       0
3  pr1gluc8NHCR2     0.0  17411.0   1315   ...         0       1       0       0
4     pr1sdm8NH1   365.0      7.0   4117   ...         0       0       0       0
5     pr1sdm8NH2  4657.0     18.0  13520   ...         0       0       0       0
6   pr1sdm8NHCR1     0.0    139.0   3451   ...         0       0       0       0
7   pr1sdm8NHCR2  1130.0   1439.0   4163   ...         0       0       0       0

I want to perform some operations on the rows and replace them , via a for loop. 我想对行执行一些操作并通过for循环替换它们。

for i in range(len(df1)):
     x=df1.iloc[i].values  ### gets all the values corresponding to each row
     x=np.vstack(x[1:]).astype(np.float) ####converts object type to a regular 2D array for all row elements except the first, which is a string.
     x=x/np.sum(x) ###normalize to 1
     df1.iloc[i,1:]=x   ###this is the step that should replace part of the old row with the new array.

But with this I get an error "ValueError: Must have equal len keys and value when setting with an ndarray". 但是有了这个,我得到一个错误“ValueError:使用ndarray设置时必须具有相等的len键和值”。 x does have the same length as each row of df1 - 1 (I don't want to replace the first column, Sample_names) x确实与df1 - 1的每一行具有相同的长度(我不想替换第一列,Sample_names)

I also tried df1=df1.replace(df1.iloc[i,1:],x) . 我也试过df1=df1.replace(df1.iloc[i,1:],x) This gives TypeError: value argument must be scalar, dict, or Series. 这给出了TypeError:value参数必须是标量,dict或Series。

I would appreciate any ideas for how to do this. 我将不胜感激任何想法如何做到这一点。

Thanks. 谢谢。

You need to reshape the x array as its shape is (n, 1) , where n is the length of your all esv-like columns. 您需要重塑x数组的形状(n, 1)因为它的形状是(n, 1) ,其中n是所有类似esv的列的长度。

Change the line: df1.iloc[i, 1:] = x to 更改行: df1.iloc[i, 1:] = x to

df1.iloc[i, 1:] = x.squeeze()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM