简体   繁体   English

将所有行更改为与第一行具有相同的数据,但一列除外

[英]Changing all rows to have same data as first except for one column

I have a dataframe called df like so:我有一个名为df的 dataframe ,如下所示:

x1  x2  x3  x4  x5  x6  x7  x8
1   0   5   6   10  11  56  7
67  10  4   87  2   34  22  12
44  16  9   9   5   11  56  7
99  82  10  6   87  9   5   8
5   54  66  7   36  3   2   7

I want to change every row to be like row 1 except for the column x1.我想将每一行更改为第 1 行,但 x1 列除外。

My expected output would be:我预期的 output 将是:

x1  x2  x3  x4  x5  x6  x7  x8
1   0   5   6   10  11  56  7
67  0   5   6   10  11  56  7
44  0   5   6   10  11  56  7
99  0   5   6   10  11  56  7
5   0   5   6   10  11  56  7

How can i do this in pandas?我怎么能在 pandas 中做到这一点?

df.loc[:, "x2":] = df.loc[0, "x2":].values
print(df)

Prints:印刷:

   x1  x2  x3  x4  x5  x6  x7  x8
0   1   0   5   6  10  11  56   7
1  67   0   5   6  10  11  56   7
2  44   0   5   6  10  11  56   7
3  99   0   5   6  10  11  56   7
4   5   0   5   6  10  11  56   7

EDIT: For column x4 :编辑:对于列x4

x4 = df["x4"].copy()
df.loc[:, :] = df.loc[0, :].values
df["x4"] = x4
print(df)

Prints:印刷:

   x1  x2  x3  x4  x5  x6  x7  x8
0   1   0   5   6  10  11  56   7
1   1   0   5  87  10  11  56   7
2   1   0   5   9  10  11  56   7
3   1   0   5   6  10  11  56   7
4   1   0   5   7  10  11  56   7
df.iloc[1:,1:]=np.nan#slice rows and columns to eliminate the first row and column and make them nan
df=df.ffill().astype(int)#forward fill the slice
print(df)

    x1  x2  x3  x4  x5  x6  x7  x8
0   1   0   5   6  10  11  56   7
1  67   0   5   6  10  11  56   7
2  44   0   5   6  10  11  56   7
3  99   0   5   6  10  11  56   7
4   5   0   5   6  10  11  56   7

With df.assign :使用df.assign

cols = ['x1'] #change list as required ex: cols = ['x1','x2']
out = df.assign(**df.drop(cols,1).iloc[0])

print(out)

   x1  x2  x3  x4  x5  x6  x7  x8
0   1   0   5   6  10  11  56   7
1  67   0   5   6  10  11  56   7
2  44   0   5   6  10  11  56   7
3  99   0   5   6  10  11  56   7
4   5   0   5   6  10  11  56   7

Use df.iloc with row range 1: assigned with row 0 .df.iloc与行范围1:分配给行0 All operating on column range 1:所有在列范围1:

df.iloc[1:, 1:] = df.iloc[0, 1:]



print(df)

   x1  x2  x3  x4  x5  x6  x7  x8
0   1   0   5   6  10  11  56   7
1  67   0   5   6  10  11  56   7
2  44   0   5   6  10  11  56   7
3  99   0   5   6  10  11  56   7
4   5   0   5   6  10  11  56   7

暂无
暂无

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

相关问题 删除所有列相同的重复行,除了熊猫中的一个 - Drop duplicated rows where all column are same except one in pandas 如何删除 csv 中第一列中包含“class”一词的所有行,但第一行除外 - how to delete all the rows of a csv that have the word "class" in the first column, except the first row that has it 用python连接具有相同第一列的csv文件的所有行 - joining all rows of a csv file that have the same first column with python 插入多行,其中insert语句的column的所有值都相同,除了一列 - Insert multiple rows, where all the values of column of insert statement are same except one column 如何将DataFrame中除第一列之外的所有列合并为一列,并删除空行? 蟒蛇 - How to merge all columns in a DataFrame except the first into one column and drop empty rows? Python 按除第一列以外的所有列分组,但聚合为列出第一列 - Group by the all the columns except the first one, but aggregate as list the first column 删除除第一行之外的所有满足条件的行 - Drop all rows that meet condition except the first one Scrapy-选择除第一个类以外的所有具有相同类的表 - Scrapy -Select all table with same class except first one Pandas 两个 dataframe 的交叉点除了一个之外几乎具有相同的列 - Intersection of Pandas two dataframe with almost have same column except one 如何在Data Frame Python中获取除一列之外的所有列? - How to take all columns except one column in Data Frame Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM