python自定义列名和长度输出_Python pandas:使用列名称向数据框添加不同长度的不同数据帧(Python pandas: Adding different dataframes with...

Python pandas:使用列名称向数据框添加不同长度的不同数据帧(Python pandas: Adding different dataframes with different length to a dataframe using name of columns)

假设我有一个包含三列的主数据框

A B C

0 7 7 7

我有三个其他数据帧,每个数据帧只有一列但长度不同。

df_A = pd.DataFrame([2,3,4,6,7,11],columns = ['A'])

df_B = pd.DataFrame([2,3,4],columns = ['B'])

df_C = pd.DataFrame([2,3,4,5,6,7,8,9,10],columns = ['C'])

如何根据列名将这些数据框中的每一个添加到主数据框中。 所以输出就是这样的

A B C

0 7 7 7

1 2 2 2

2 3 3 3

3 4 4 4

4 6 NaN 5

5 7 NaN 6

6 11 NaN 7

7 NaN NaN 8

8 NaN NaN 9

9 NaN NaN 10

在此先感谢您的帮助!

Suppose I have a main dataframe with three columns

A B C

0 7 7 7

And I have three other dataframes, each one has only one column but with different length.

df_A = pd.DataFrame([2,3,4,6,7,11],columns = ['A'])

df_B = pd.DataFrame([2,3,4],columns = ['B'])

df_C = pd.DataFrame([2,3,4,5,6,7,8,9,10],columns = ['C'])

How can I add each one of these dataframes to the main dataframe based on the name of column. So the output will be like this

A B C

0 7 7 7

1 2 2 2

2 3 3 3

3 4 4 4

4 6 NaN 5

5 7 NaN 6

6 11 NaN 7

7 NaN NaN 8

8 NaN NaN 9

9 NaN NaN 10

Thanks in advance for your help!

原文:https://stackoverflow.com/questions/40639759

2020-09-11 15:09

满意答案

试试这个:

In [187]: pd.concat([df, df_A.join(df_B, how='outer').join(df_C, how='outer')])

Out[187]:

A B C

0 7.0 7.0 7

0 2.0 2.0 2

1 3.0 3.0 3

2 4.0 4.0 4

3 6.0 NaN 5

4 7.0 NaN 6

5 11.0 NaN 7

6 NaN NaN 8

7 NaN NaN 9

8 NaN NaN 10

try this:

In [187]: pd.concat([df, df_A.join(df_B, how='outer').join(df_C, how='outer')])

Out[187]:

A B C

0 7.0 7.0 7

0 2.0 2.0 2

1 3.0 3.0 3

2 4.0 4.0 4

3 6.0 NaN 5

4 7.0 NaN 6

5 11.0 NaN 7

6 NaN NaN 8

7 NaN NaN 9

8 NaN NaN 10

2016-11-16

相关问答

有人可能会有一个更优雅的解决方案,但在我的脑海中,我会加入df2到df1两次,以便将所有内容都放入一个数据集中,并且比较非常简单。 df2基本上是一个查找表,并且df2.chr应该分别与df1.chr1和df1.chr2匹配。 df_all = df1.merge(df2,

how='inner',

left_on='chr1',

right_on='chr') \

...

df2.loc['new_row'] = list

假设'new_row'不在cont_index 。 df2.loc['new_row'] = list

Assuming 'new_row' is not in cont_index.

你的描述有点模糊(因此我的评论)。 首先,您要做的是选择要搜索的数据帧的行: dftmp = df1[(df1.afleverdag==x1) & (df1.ind_init_actie=='N')]

这样你就不会为循环中的每个项目执行此操作。 其次,使用.groupby 。 newseries = dftmp['aantal_colli'].groupby(dftmp.index).sum()

newseries = newseries.ix[df.collonr.unique()]

Y...

也许你的索引不匹配。 尝试使用ignore_index参数: full_table = pd.concat([intersection, links], axis=1, ignore_index=True)

Maybe your indexes don't match up. Try using the ignore_index parameter: full_table = pd.concat([intersection, links], axis=1, ignore_index=True)

关于什么 df1[df1.frame.isin(df2.frame)]

Out:

frame

1 2

2 3

df2[df2.frame.isin(df1.frame)]

Out:

frame

0 2

1 3

what about df1[df1.frame.isin(df2.frame)]

Out:

frame

1 2

2 3

df2[df2.frame.isin(df1.frame)]

Out:

...

尝试添加orient='index' : new_df = pandas.DataFrame.from_dict(mia_classe, orient='index')

使用简易示例: mia_classe = {'classe': 'classe','media': 100,'deviazione': 150}

new_df = pd.DataFrame.from_dict(mia_classe, orient='index')

0

classe ...

如果您知道两者之间的索引相同而您不关心列名,那么只需执行以下操作: DataFrame(df1.values + df2.values, df1.index, df1.columns)

If you know the index is the same between the two and you don't care about the column names, just do: DataFrame(df1.values + df2.values, df1.index, df1.colum...

我建议您使用DataFrame API,它允许在连接 , 合并 , 分组等方面使用DF。您可以在下面找到我的解决方案: import pandas as pd

df1 = pd.DataFrame({'Column1': [1,2,3,4,5],

'Column2': ['a','b','c','d','e'],

'Column3': ['r','u','k','j','f']})

df2 = pd.DataFrame({'Column1': [1,1,1,2,2,3,3]...

试试这个: In [187]: pd.concat([df, df_A.join(df_B, how='outer').join(df_C, how='outer')])

Out[187]:

A B C

0 7.0 7.0 7

0 2.0 2.0 2

1 3.0 3.0 3

2 4.0 4.0 4

3 6.0 NaN 5

4 7.0 NaN 6

5 11.0 NaN 7

6 NaN NaN 8

7 ...

你可以使用带boolean indexing loc进行select,然后使用0.9乘以: df.loc[df.Brewery == 'Glenfiddich', 'Store Price'] *= .9

样品: print (df)

Brewery Store Price

104 Glenfiddich 109.99

105 Glenfiddich 89.99

120 Another 100.00

df.loc[df.B...

相关文章

abs(x) 说明:abs(x)返回x的绝对值,如果参数是复数,则返回复数的模; 参数x:整

...

列表就像java里的collection,所具有的特性也要比元组更多,更灵活,其character总结

...

pychseg - A Python Chinese Segment Project - Google

...

简单的数据类型以及赋值 1)变量不需要声明 Python的变量不需要声明,你可以直接输入:>>>a =

...

Python 编程语言具有很高的灵活性,它支持多种编程方法,包括过程化的、面向对象的和函数式的。但最重

...

python2和python3的区别,1.性能 Py3.0运行 pystone benchmark的速

...

python的官网:http://www.python.org/ 有两个版本,就像struts1和st

...

Python的文件类型 Python有三种文件类型,分别是源代码文件、字节码文件和优化代码文件

源代

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值