一行中在pandas DataFrame的单行中分配多个列值 [英] Assigning multiple column values in a single row of pandas DataFrame, in one line
本文介绍了一行中在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上完成的,而不是在副本(和收集的垃圾)上完成的.
您只能指定某些列:
In [12]: df.loc[3, list('ABCDE')] = ['V1', 4.3, 2.2, 2.2, 20.2]
这篇关于一行中在pandas DataFrame的单行中分配多个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文