在pandas DataFrame中分配多个值

时间:2015-06-24 03:10:29

标签: indexing pandas lambda

将我的数据框按“小时”分组。第一列计算了一些以小时列为条件的值。

public class DisposeExample
{
    // A base class that implements IDisposable. 
    // By implementing IDisposable, you are announcing that 
    // instances of this type allocate scarce resources. 
    public class MyResource: IDisposable
    {
        // Pointer to an external unmanaged resource. 
        private IntPtr handle;
        // Other managed resource this class uses. 
        private Component component = new Component();
        // Track whether Dispose has been called. 
        private bool disposed = false;

        // The class constructor. 
        public MyResource(IntPtr handle)
        {
            this.handle = handle;
        }

        // Implement IDisposable. 
        // Do not make this method virtual. 
        // A derived class should not be able to override this method. 
        public void Dispose()
        {
            Dispose(true);
            // This object will be cleaned up by the Dispose method. 
            // Therefore, you should call GC.SupressFinalize to 
            // take this object off the finalization queue 
            // and prevent finalization code for this object 
            // from executing a second time.
            GC.SuppressFinalize(this);
        }

        // Dispose(bool disposing) executes in two distinct scenarios. 
        // If disposing equals true, the method has been called directly 
        // or indirectly by a user's code. Managed and unmanaged resources 
        // can be disposed. 
        // If disposing equals false, the method has been called by the 
        // runtime from inside the finalizer and you should not reference 
        // other objects. Only unmanaged resources can be disposed. 
        protected virtual void Dispose(bool disposing)
        {
            // Check to see if Dispose has already been called. 
            if(!this.disposed)
            {
                // If disposing equals true, dispose all managed 
                // and unmanaged resources. 
                if(disposing)
                {
                    // Dispose managed resources.
                    component.Dispose();
                }

                // Call the appropriate methods to clean up 
                // unmanaged resources here. 
                // If disposing is false, 
                // only the following code is executed.
                CloseHandle(handle);
                handle = IntPtr.Zero;

                // Note disposing has been done.
                disposed = true;

            }
        }

        // Use interop to call the method necessary 
        // to clean up the unmanaged resource.
        [System.Runtime.InteropServices.DllImport("Kernel32")]
        private extern static Boolean CloseHandle(IntPtr handle);

        // Use C# destructor syntax for finalization code. 
        // This destructor will run only if the Dispose method 
        // does not get called. 
        // It gives your base class the opportunity to finalize. 
        // Do not provide destructors in types derived from this class.
        ~MyResource()
        {
            // Do not re-create Dispose clean-up code here. 
            // Calling Dispose(false) is optimal in terms of 
            // readability and maintainability.
            Dispose(false);
        }
    }
    public static void Main()
    {
        // Insert code here to create 
        // and use the MyResource object.
    }
}

问题是如何将这些值分配给我的主数据帧依赖于' Hour'柱。 我自己尝试了什么:

Out[15]: 
    normalized_entries  Hour
3             0.000563     3
6             0.001265     6
23            0.002392    23
7             0.002655     7
2             0.002962     2
15            0.003095    15
11            0.004472    11
19            0.005776    19
14            0.008059    14
5             0.008163     5
22            0.008319    22
10            0.009102    10
18            0.011684    18
4             0.016871     4
1             0.034377     1
8             0.038017     8
13            0.065110    13
0             0.074780     0
9             0.076391     9
17            0.087821    17
21            0.090782    21
16            0.119952    16
12            0.157843    12
20            0.169550    20

我认为这应该用lambda map解决,但无法弄清楚如何。 谢谢,Donatas

2 个答案:

答案 0 :(得分:0)

我想你可能想要使用合并,例如:

pd.merge(data, m2, on = 'Hour')

答案 1 :(得分:0)

这将适合您并保持索引:

data['normalized_entries']=data['Hour'].map(m2.set_index('Hour').to_dict()['normalized_entries'])