如何根据熊猫的时间范围用数据填充新列

时间:2020-01-31 18:10:28

标签: python python-3.x pandas numpy datetime

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class DirChooser extends JPanel implements ActionListener {

    JButton button;
    static JFileChooser chooser = null;

    public DirChooser() {
        button = new JButton("Select Directory");
        button.addActionListener (this);
        add(button);
    }

    public static void main (String [] args) {

        String path = getDir();

        // ... code ...

    }

    public static String getDir() {

        String dir = null;
        JFrame frame = new JFrame("");
        DirChooser panel = new DirChooser();
        frame.addWindowListener(
                new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                }

            );
        frame.getContentPane().add(panel,"Center");
        frame.setSize(panel.getPreferredSize());
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        promtENTERKey();  

        dir = chooser.getSelectedFile().getAbsolutePath();
        frame.dispose();    
        return dir;
    }

    public Dimension getPreferredSize(){

        return new Dimension(200, 100);
    }

    public void actionPerformed (ActionEvent e) {

        chooser = new JFileChooser (); 
        chooser.setCurrentDirectory (new java.io.File("."));
        chooser.setDialogTitle ("choose directory");
        chooser.setFileSelectionMode (JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed (false); // disable the "All files" option.

        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
            System.out.println ("\n" + "Directory has been selected");
        }
        else {
            System.out.println("No Selection ");
        }
    }

    public static void promtENTERKey() {

        System.out.println("\n" + "Select directory first and then press \"ENTER\" to continue...");
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);
        scanner.nextLine();
    }
}

然后如何向该字段添加新列,在该列中可以指定每个小时分组的单元格。例如,在00:00:00到00:59:59内的所有交易都以1填充,在01:00:00到01:59:59内的交易以2填充,依此类推直到23:00 :00到23:59:59以填充24。

Time_duration = df['period']
print (Time_duration)

0        23:59:56
1        23:59:56
2        23:59:55
3        23:59:53
4        23:59:52
           ...
74187    00:00:18
74188    00:00:09
74189    00:00:08
74190    00:00:03
74191    00:00:02

2 个答案:

答案 0 :(得分:0)

import datetime as dt

def hr_rt(t_str=str):
    time = dt.datetime.strptime(t_str, "%H:%M:%S")
    return time.hour + 1
df3['hr'] = df3.time.apply(hr_rt)


 sr_no      time  hr
0      0  23:59:56  24
1      1  23:59:56  24
2      2  23:59:55  24
3      3  23:59:53  24
4      4  23:59:52  24

我想这应该有所帮助

答案 1 :(得分:0)

确保列为Timedelta,然后使用整数除一小时。

#df['period'] = pd.to_timedelta(df['period'])

df['hour'] = (df['period'] // pd.Timedelta(hours=1)) + 1

    period  hour
0 23:59:56    24
1 23:59:56    24
2 23:59:55    24
3 23:59:53    24
4 23:59:52    24
5 00:00:18     1
6 00:00:09     1
7 00:00:08     1
8 00:00:03     1
9 00:00:02     1