我已经创建了一个熊猫数据框架
df = DataFrame(index=['A','B','C'], columns=['x','y'])
得到了这个
x y
A NaN NaN
B NaN NaN
C NaN NaN
现在,我想给特定的单元格赋值,例如给C行和x列赋值。
我希望得到这样的结果:
x y
A NaN NaN
B NaN NaN
C 10 NaN
下面的代码:
df.xs('C')['x'] = 10
但是df的内容没有改变。数据帧仍然只包含nan。
有什么建议吗?
除了上面的答案之外,这里还有一个基准测试,比较了向已有的数据框架添加数据行的不同方法。它表明使用at或set-value对于大数据帧是最有效的方法(至少对于这些测试条件)。
为每一行创建新的数据框架,然后…
... 追加它(13.0 s)
... 串联它(13.1秒)
首先将所有新行存储在另一个容器中,转换为新数据帧一次,然后追加…
容器=列表的列表(2.0 s)
容器=列表字典(1.9 s)
预分配整个数据框架,遍历新行和所有列,并使用填充
... (0.6秒)
... Set_value (0.4 s)
在测试中,使用了包含100,000行和1,000列的现有数据框架和随机numpy值。在这个数据框架中,添加了100个新行。
代码见下文:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 21 16:38:46 2018
@author: gebbissimo
"""
import pandas as pd
import numpy as np
import time
NUM_ROWS = 100000
NUM_COLS = 1000
data = np.random.rand(NUM_ROWS,NUM_COLS)
df = pd.DataFrame(data)
NUM_ROWS_NEW = 100
data_tot = np.random.rand(NUM_ROWS + NUM_ROWS_NEW,NUM_COLS)
df_tot = pd.DataFrame(data_tot)
DATA_NEW = np.random.rand(1,NUM_COLS)
#%% FUNCTIONS
# create and append
def create_and_append(df):
for i in range(NUM_ROWS_NEW):
df_new = pd.DataFrame(DATA_NEW)
df = df.append(df_new)
return df
# create and concatenate
def create_and_concat(df):
for i in range(NUM_ROWS_NEW):
df_new = pd.DataFrame(DATA_NEW)
df = pd.concat((df, df_new))
return df
# store as dict and
def store_as_list(df):
lst = [[] for i in range(NUM_ROWS_NEW)]
for i in range(NUM_ROWS_NEW):
for j in range(NUM_COLS):
lst[i].append(DATA_NEW[0,j])
df_new = pd.DataFrame(lst)
df_tot = df.append(df_new)
return df_tot
# store as dict and
def store_as_dict(df):
dct = {}
for j in range(NUM_COLS):
dct[j] = []
for i in range(NUM_ROWS_NEW):
dct[j].append(DATA_NEW[0,j])
df_new = pd.DataFrame(dct)
df_tot = df.append(df_new)
return df_tot
# preallocate and fill using .at
def fill_using_at(df):
for i in range(NUM_ROWS_NEW):
for j in range(NUM_COLS):
#print("i,j={},{}".format(i,j))
df.at[NUM_ROWS+i,j] = DATA_NEW[0,j]
return df
# preallocate and fill using .at
def fill_using_set(df):
for i in range(NUM_ROWS_NEW):
for j in range(NUM_COLS):
#print("i,j={},{}".format(i,j))
df.set_value(NUM_ROWS+i,j,DATA_NEW[0,j])
return df
#%% TESTS
t0 = time.time()
create_and_append(df)
t1 = time.time()
print('Needed {} seconds'.format(t1-t0))
t0 = time.time()
create_and_concat(df)
t1 = time.time()
print('Needed {} seconds'.format(t1-t0))
t0 = time.time()
store_as_list(df)
t1 = time.time()
print('Needed {} seconds'.format(t1-t0))
t0 = time.time()
store_as_dict(df)
t1 = time.time()
print('Needed {} seconds'.format(t1-t0))
t0 = time.time()
fill_using_at(df_tot)
t1 = time.time()
print('Needed {} seconds'.format(t1-t0))
t0 = time.time()
fill_using_set(df_tot)
t1 = time.time()
print('Needed {} seconds'.format(t1-t0))
使用index with condition的一种方法是首先获取满足条件的所有行的索引,然后简单地以多种方式使用这些行索引
conditional_index = df.loc[ df['col name'] <condition> ].index
示例条件如下
==5, >10 , =="Any string", >= DateTime
然后您可以以各种方式使用这些行索引,例如
替换conditional_index的一列值
df.loc[conditional_index , [col name]]= <new value>
替换conditional_index的多列值
df.loc[conditional_index, [col1,col2]]= <new value>
保存conditional_index的一个好处是,您可以将一个列的值分配给具有相同行索引的另一个列
df.loc[conditional_index, [col1,col2]]= df.loc[conditional_index,'col name']
这一切都是可能的,因为.index返回一个索引数组,.loc可以直接寻址,这样就避免了一次又一次的遍历。