如何在熊猫中累计时间

时间:2018-09-27 14:45:22

标签: pandas dataframe timestamp string-aggregation

我在大型csv文件中有一列,其时间戳记格式为%H:%M:%S。从时间戳列表中获取汇总(例如平均值,中位数,25%,75%等)的最佳方法是什么。

    const openJsonFile = (file) => {
        return new Promise(function(resolve, reject) {
            const _ = new XMLHttpRequest();
            _.overrideMimeType("application/json");
            _.open("GET", file, true);
            _.onload = function() {
                if(this.readyState == 4) {
                    if(this.status >= 200 && this.status < 300) {
                        resolve(_.responseText);
                    } else {
                        reject(_.statusText);
                    }
                }
            };
            _.onerror = function () {
                reject(_.statusText);
            };
            _.send();
        });
    };

    // Create a map
    const map = L.map('map').setView([55.92, 37.83], 12);

    // Add Mapbox tiles to map
    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiYXJmZW8iLCJhIjoiY2pjeGw5bG5nMTF5ZjMzczZoZWVzbWdyNSJ9.Yh3u2uEHErWpTvAg3Ak_qw', {
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
        maxZoom: 20,
        id: 'mapbox.streets',
        accessToken: 'pk.eyJ1IjoiYXJmZW8iLCJhIjoiY2pjeGw5bG5nMTF5ZjMzczZoZWVzbWdyNSJ9.Yh3u2uEHErWpTvAg3Ak_qw'
    }).addTo(map);

    // Open and parse heojson file
    openJsonFile("output.geojson")
        .then(function(json) {
            const data = JSON.parse(json);

            // Draw GeoJSON features on map
            for(const feature of data.features) {
                const x = L.geoJSON(feature).addTo(map);

                // If a feature is Point, add a popup (if applicable)
                if(feature.geometry.type === "Point")
                    x.bindPopup((name = feature.properties.name) ? name : null);
            }
        })
        .catch(function(error) {
            console.error("Error:", error);
        });

1 个答案:

答案 0 :(得分:1)

您可以使用time delta将该字符串转换为时间段,它将允许arithmetic operation在时间戳上进行加减

df.time1 = pd.to_timedelta(df.time1)
df.sum()

出局:

key                  abc
time1    1 days 08:32:46
value                 12
dtype: object

但是对于包含mean and other的乘法和除法,您可以转换timestamp to seconds并应用

df.time1 = pd.to_timedelta(df.time1)/np.timedelta64(1, 's')

出局:

    key time1   value
0   a   34241.0 5
1   b   36056.0 4
2   c   46869.0 3

完成所需的操作后,您可以使用pd.to_timedelta with 'seconds' u nit

转换回时间戳。
pd.to_timedelta(df.time1,unit='s')

出局:

0   09:30:41
1   10:00:56
2   13:01:09
Name: time1, dtype: timedelta64[ns]