用xarray python合并CSV文件

时间:2019-05-01 07:44:02

标签: python csv merge nan python-xarray

我有三个csv文件,它们具有相同的格式,但它们用于不同的时间。我使用第一个csv文件的xarray创建了三维数据集xr dataset。下一步,我想将其他csv文件合并到我的xr数据集中。您可以在下面看到我的代码。 问题:在最后的nan中,我有Lat;Lon;Alt;# Date;UT time;Temperature;Relative Humidity;Wind speed;Wind direction;Short-wave irradiation 29.893;48.859;1;2018-01-01;01:00;290.09;59.83;1.67;174.74;0.0 29.893;48.859;1;2018-01-01;02:00;289.72;66.13;2.06;166.59;0.0 29.893;48.859;1;2018-01-01;03:00;289.34;74.27;2.5;161.42;0.0 29.893;48.859;1;2018-01-01;04:00;289.11;82.31;2.88;158.44;4.349 29.893;48.859;1;2018-01-01;05:00;289.88;84.44;2.97;158.09;118.2629 29.893;48.859;1;2018-01-01;06:00;291.48;79.04;3.36;160.37;300.52个变量值。有什么错误?

每个csv文件中的数据样本在不同的纬度和时间范围内,但是时间相同。

import pandas as pd import xarray #import Data from .csv file colnames = ['Lat','Lon','Alt','# Date','UT time','Temperature', 'Relative Humidity', 'Wind speed', 'Wind direction', 'Short-wave irradiation'] df = pd.read_csv('data/pnt_29.887_48.497.csv',header=0,error_bad_lines=False, names = colnames, sep=';') # create new column based on day and time, new columnn = datetime s = df['# Date'].astype(str).str.zfill(2) + ' ' + df['UT time'].astype(str).str.zfill(2) df['datetime'] = pd.to_datetime(s, errors='ignore', format='%Y%m%d%H') del df['# Date'] del df['UT time'] xr = df.set_index(['datetime', 'Lat', 'Lon']).to_xarray()

代码

import os    
csv_list = []
for root, dirs, files in os.walk(r'H:\Anaconda_VSCode\Data'):
    for file in files:
        if file.endswith('.csv'):
            csv_list.append(file)

列出一个csv文件。

for file in csv_list:
    #import Data from .csv file
    colnames = ['Lat','Lon','Alt','# Date','UT time','Temperature', 'Relative Humidity', 'Wind speed', 'Wind direction', 'Short-wave irradiation']
    df = pd.read_csv('data/'+ file ,header=0,error_bad_lines=False, names = colnames, sep=';')
    # create new column based on day and time, new columnn = datetime
    s = df['# Date'].astype(str).str.zfill(2) + ' ' + df['UT time'].astype(str).str.zfill(2)
    df['datetime'] = pd.to_datetime(s, errors='ignore', format='%Y%m%d%H')
    del df['# Date']
    del df['UT time']
    xr_tmp = df.set_index(['datetime', 'Lat', 'Lon']).to_xarray()
    xr = xarray.merge([xr_tmp, xr])
    #xr = xarray.concat((xr_tmp,xr))
    del (xr_tmp)
xr

合并:

nan

具有<xarray.Dataset> Dimensions: (Lat: 3, Lon: 3, datetime: 8760) Coordinates: * Lat (Lat) float64 29.89 29.89 29.89 * Lon (Lon) float64 48.5 48.55 48.6 * datetime (datetime) object '2018-01-01 01:00' ... '2018-12-31 24:00' Data variables: Alt (datetime, Lat, Lon) float64 1.0 nan nan ... nan 1.0 Temperature (datetime, Lat, Lon) float64 288.4 nan ... nan 284.5 Relative Humidity (datetime, Lat, Lon) float64 62.27 nan ... nan 76.0 Wind speed (datetime, Lat, Lon) float64 2.63 nan ... nan 5.15 Wind direction (datetime, Lat, Lon) float64 184.7 nan ... nan 335.1 Short-wave irradiation (datetime, Lat, Lon) float64 0.0 nan nan ... nan 0.0 值的结果:

import android.content.Context
import android.util.AttributeSet
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import android.view.Gravity

class CenteredToolbar(context: Context, attrs: AttributeSet?, defStyleAttr: Int):
Toolbar(context, attrs, defStyleAttr) {

constructor(context: Context) : this(context, null, 0)

constructor(context: Context, attrs: AttributeSet) : this(context, attrs, androidx.appcompat.R.attr.toolbarStyle)

override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
    super.onLayout(changed, l, t, r, b)

    val childCount = childCount
    for (i in 0 until childCount) {
        val view = this.getChildAt(i)
        if (view is TextView) {
            forceTitleCenter(view,l, r)
            break
        }
    }
}

/**
 * Centering the layout.
 *
 * @param view The view to be centered
 */
private fun forceTitleCenter(view: TextView, l: Int,  r: Int) {
    val top = view.getTop()
    val bottom = view.getBottom()
    view.layout(l, top, r, bottom)
    navigationIcon?.let{
        view.setPadding(it.intrinsicWidth,0,0,0)
    }
    view.gravity = Gravity.CENTER
}

0 个答案:

没有答案