用给定属性在数据框中添加列
我有以下数据框架,
df = pd.DataFrame({'a': [[5.4, 15.3],[2.0,9.1]],'b':[[9.8,2.0],[9.7,6.0,1.2]]})
我想使用分配函数添加列“ C”,以便“ C”中的每个元素是将“ A”中相应列表的元素加倍后获得的元素列表。 我写了以下代码”,
df.assign(c=np.array(df['a'])*2)
但它给出了错误的输出。 我期望输出如下所示:
a b c
0 [5.4, 15.3] [9.8, 2.0] [10.8, 30.6]
1 [2.0, 9.1] [9.7, 6.0, 1.2] [4.0, 18.2]
I have the following dataframe,
df = pd.DataFrame({'a': [[5.4, 15.3],[2.0,9.1]],'b':[[9.8,2.0],[9.7,6.0,1.2]]})
I want to add a column 'c' using assign function such that each element in 'c' is a list of elements we get after doubling up the elements of corresponding list in 'a'.
I wrote the below code'
df.assign(c=np.array(df['a'])*2)
But it gave the wrong output.
I'm expecting the output as shown below:
a b c
0 [5.4, 15.3] [9.8, 2.0] [10.8, 30.6]
1 [2.0, 9.1] [9.7, 6.0, 1.2] [4.0, 18.2]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您几乎可以使用列表理解来创建一个新列表,并分配给DF ['c'],如下所示;
You are almost there you can utilize list comprehension to create a new list and assign to df['c'] as follows;
添加新列真的很容易
It is really easy to add new column