如何用其他系列替换pandas数据帧的子集

时间:2017-02-12 12:41:01

标签: python pandas data-analysis missing-data

我认为这是一个微不足道的问题,但我不能让它发挥作用。

d = {   'one': pd.Series([1,2,3,4], index=['a', 'b', 'c', 'd']),
            'two': pd.Series([np.nan,6,np.nan,8], index=['a', 'b', 'c', 'd']),
            'three': pd.Series([10,20,30,np.nan], index = ['a', 'b', 'c', 'd'])}         
   ​    
df = pd.DataFrame(d)
df

    one     three   two
a   1       10.0    NaN
b   2       20.0    6.0
c   3       30.0    NaN
d   4       NaN     8.0

我的服务:

​fill = pd.Series([30,60])

我想更换一个特定的列,让它成为两个'。我的系列名为fill,其中列为“两个”字样。满足条件:是南。残友对我有帮助吗?  我想要的结果:

df

    one     three   two
a   1       10.0    30
b   2       20.0    6.0
c   3       30.0    60
d   4       NaN     8.0

1 个答案:

答案 0 :(得分:6)

我认为您需要loc numpy array fill替换df.loc[df.two.isnull(), 'two'] = fill.values print (df) one three two a 1 10.0 30.0 b 2 20.0 6.0 c 3 30.0 60.0 d 4 NaN 8.0 const string FileName = "MyFile.db"; string DbDir; string DbPath; Constructor: DbDir = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("test").Path; DbPath = Path.Combine (DbDir, DbFileName); public SQLite.Net.Async.SQLiteAsyncConnection GetConnectionAsync () { var connectionFactory = new Func<SQLite.Net.SQLiteConnectionWithLock>(()=> new SQLite.Net.SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLite.Net.SQLiteConnectionString(DbPath, storeDateTimeAsTicks: false))); var asyncConnection = new SQLiteAsyncConnection(connectionFactory); return asyncConnection; } isnull创建的<{1}}:

public async Task<Parms> ReadParmsAsync ()
{
    var db = GetConnectionAsync ();
    var query = db.Table<Parms> ().Where (p => p.Id == 1);
    return await query.FirstOrDefaultAsync ();
}