在一行中将多个列值分配给python pandas DataFrame [英] Assigning multiple column values to a python pandas DataFrame in one line

查看:186
本文介绍了在一行中将多个列值分配给python pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将多个值分配给DataFrame中的单个行,并且我需要正确的语法。

I'm trying to Assign multiple values to a single row in a DataFrame and I need the correct syntax.

请参阅以下代码。

import pandas as pd

df = pd.DataFrame({
'A': range(10),
'B' : '',
'C' : 0.0,
'D' : 0.0,
'E': 0.0,
})

#Works fine
df['A'][2] = 'tst'

#Is there a way to assign multiple values in a single line and if so what is the correct syntax
df[['A', 'B', 'C', 'D', 'E']][3] = ['V1', 4.3, 2.2, 2.2, 20.2]

感谢您的帮助

推荐答案

使用 loc (并避免链接):

Use loc (and avoid chaining):

In [11]: df.loc[3] = ['V1', 4.3, 2.2, 2.2, 20.2]

这样可以确保在DataFrame上进行分配,而不是复制(和垃圾回收)。 em>

This ensures the assigning is done inplace on the DataFrame, rather than on a copy (and garbage collected).

你可以仅指定某些列:

 In [12]: df.loc[3, list('ABCDE')] = ['V1', 4.3, 2.2, 2.2, 20.2]

这篇关于在一行中将多个列值分配给python pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆