我已经创建了一个熊猫数据框架
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))
已弃用Set_value()。
从0.23.4版本开始,Pandas“宣布了未来”…
>>> df
Cars Prices (U$)
0 Audi TT 120.0
1 Lamborghini Aventador 245.0
2 Chevrolet Malibu 190.0
>>> df.set_value(2, 'Prices (U$)', 240.0)
__main__:1: FutureWarning: set_value is deprecated and will be removed in a future release.
Please use .at[] or .iat[] accessors instead
Cars Prices (U$)
0 Audi TT 120.0
1 Lamborghini Aventador 245.0
2 Chevrolet Malibu 240.0
考虑到这些建议,以下是如何使用它们的演示:
按行/列整数位置
>>> df.iat[1, 1] = 260.0
>>> df
Cars Prices (U$)
0 Audi TT 120.0
1 Lamborghini Aventador 260.0
2 Chevrolet Malibu 240.0
通过行/列标签
>>> df.at[2, "Cars"] = "Chevrolet Corvette"
>>> df
Cars Prices (U$)
0 Audi TT 120.0
1 Lamborghini Aventador 260.0
2 Chevrolet Corvette 240.0
引用:
pandas.DataFrame.iat
pandas.DataFrame.at
.iat /。是很好的解决办法。
假设你有这样一个简单的数据帧:
A B C
0 1 8 4
1 3 9 6
2 22 33 52
如果我们想修改单元格[0,"A"]的值,你可以使用这些解决方案之一:
df。Iat [0,0] = 2
df。at[0,'A'] = 2
下面是一个如何使用iat获取和设置cell值的完整示例:
def prepossessing(df):
for index in range(0,len(df)):
df.iat[index,0] = df.iat[index,0] * 2
return df
Y_train前:
0
0 54
1 15
2 15
3 8
4 31
5 63
6 11
Y_train调用prepossession函数后,iat改变每个单元格的值乘以2:
0
0 108
1 30
2 30
3 16
4 62
5 126
6 22