简体   繁体   English

Pandas:如何将新列附加到数据框中的所有行

[英]Pandas: How to append new columns to all rows in data frame

I have two data frames: 我有两个数据框:

df1 = pd.DataFrame(data={
  'ColumnA': ['A1','A2','A3'],
  'ColumnB' : [ 'B1','B2','B3'],
  'ColumnC' : [ 'C1','C2','C3']
})


df2 = pd.DataFrame(data={
  'id': ['1'],
  'Value1' : [ 'v1'],
  'Value2' : [ 'v2']
})



  ColumnB ColumnC columnA id
0      B1      C1      A1  1
1      B2      C2      A2  1
2      B3      C3      A3  1


  Value1 Value2 id
0     v1     v2  1

And looking to get df3 where all rows on df1 will have columns from df2. 并希望获得df3,其中df1上的所有行都将包含来自df2的列。

  ColumnB ColumnC columnA id Value1 Value2
0      B1      C1      A1  1     v1     v2
1      B2      C2      A2  1     v1     v2
2      B3      C3      A3  1     v1     v2

Currently im doing it this way: 目前我这样做:

id = df2['id'][0]
df1['id'] = id

df3 = df1.merge(df2,left_on='id',right_on='id',how='left')

What is the better way to do it? 有什么更好的方法呢?

Use assign by Series created by selected first row of df2 : 使用由所选第一行df2创建的Series assign

df3 = df1.assign(**df2.iloc[0])
print (df3)
  ColumnA ColumnB ColumnC Value1 Value2 id
0      A1      B1      C1     v1     v2  1
1      A2      B2      C2     v1     v2  1
2      A3      B3      C3     v1     v2  1

Using pd.concat with ffill() pd.concatffill()

pd.concat([df1,df2],axis=1).ffill()
Out[388]: 
  ColumnA ColumnB ColumnC Value1 Value2 id
0      A1      B1      C1     v1     v2  1
1      A2      B2      C2     v1     v2  1
2      A3      B3      C3     v1     v2  1

Another way 其他方式

In [1728]: df1.assign(k=0).merge(df2.assign(k=0), on='k').drop('k', 1)
Out[1728]:
  ColumnA ColumnB ColumnC Value1 Value2 id
0      A1      B1      C1     v1     v2  1
1      A2      B2      C2     v1     v2  1
2      A3      B3      C3     v1     v2  1

If you don't have NaN in data. 如果您没有数据中的NaN

In [1734]: df1.join(df2).ffill()
Out[1734]:
  ColumnA ColumnB ColumnC Value1 Value2 id
0      A1      B1      C1     v1     v2  1
1      A2      B2      C2     v1     v2  1
2      A3      B3      C3     v1     v2  1

暂无
暂无

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

相关问题 如何在Pandas Python中将任意行附加到新数据框中? - How to append arbitrary rows into a new data frame in pandas python? 如何 append 数据帧的所有行的某些列到另一个 - How to append certain columns of all rows of data frame to another 如何遍历满足某些条件的 Pandas 数据框并将这些行附加到新数据框? - How to iterate through pandas data frame that meet some condition and append those rows to new data frame? 使用Python Pandas获取数据框中的所有列的名称和行数 - Getting the names of all the columns and number of rows in a data frame with Python Pandas 评估所有列后删除熊猫数据框中的行 - Deleting rows in pandas data frame after evaluating all columns 在 pandas 数据帧的行中添加时间,在数据帧的新列中添加 append total_time - add time in rows of pandas data frame and append total_time in new column of data frame 如何使用 Pandas 数据框的特定行和列创建新系列? - How can I create a new series by using specific rows and columns of a pandas data frame? 如何将列表附加到pandas数据框作为新行 - how to append a list to pandas data frame as new row 如何在数据框某些行的所有列上使用熊猫套用功能 - How to use pandas apply function on all columns of some rows of data frame 如何使用 Python 过滤 Pandas 数据帧中所有或部分行值大于 0 的列? - How to filter columns whose all or some rows values are greater than 0 in Pandas data-frame using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM