在Python Pandas中指定多指数标头时,请使用USECOL

发布于 2025-02-09 09:24:45 字数 716 浏览 1 评论 0原文

我有一个庞大的数据可以根据两个标头读取,但是当我使用多指数方法时,我无法在pandas dataframe中使用“ usecols”。

当我使用

df = pd.read_csv(files, delimiter=' ', header=[0,1])

它时,会花费太多时间和内存。

我试图使用的另一种方法是

df = pd.read_csv(files, delimiter=' ', usecols = ["80.375"])

仅采用一列,而应使用所有四个列,带有'80 .375'

所需输出

“在此处输入图像说明”

建议您的其他方法

请提前

I have a huge data to read based on two headers, But when I am using multi-index approach I am unable to use 'usecols' in pandas dataframe.

enter image description here

When I am using

df = pd.read_csv(files, delimiter=' ', header=[0,1])

it is taking too much of time and memory.

Another approach I am trying to use is

df = pd.read_csv(files, delimiter=' ', usecols = ["80.375"])

it is taking only one column, rather it should take all the four columns with header '80.375'

Desired output

enter image description here

Please suggest any alternative approach

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲世九天 2025-02-16 09:24:46

您可以使用两个通过来提取数据和标题。

# read_csv common options
opts = {'sep': ' ', 'header': None}

# Extract headers, create MultiIndex
headers = pd.read_csv('data.csv', **opts, nrows=2)
mi = pd.MultiIndex.from_frame(headers.T)

# Keep desired columns
dti = [0, 1, 2]  # Year, Month, Day
cols = mi.get_locs([80.375]).tolist()

# Build dataframe
df = pd.read_csv('data.csv', **opts, skiprows=2, index_col=dti, usecols=dti+cols)
df.columns = mi[cols]
df = df.rename_axis(index=['Year', 'Month', 'Day'], columns=['Lvl1', 'Lvl2'])
df.index = pd.to_datetime(df.index.to_frame()).rename('DateTime')

输出:

>>> df
Lvl1       80.375                     
Lvl2       28.625 28.875 29.125 29.375
DateTime                              
2015-01-01     21     22     23     24
2015-01-02     31     32     33     34
2015-01-03     41     42     43     44
2015-01-04     51     52     53     54

输入CSV文件:

   80.125 80.375 80.375 80.375 80.375 80.625
   28.875 28.625 28.875 29.125 29.375 28.875
2015 1 1 20 21 22 23 24 25
2015 1 2 30 31 32 33 34 35
2015 1 3 40 41 42 43 44 45
2015 1 4 50 51 52 53 54 55

更新

我需要在单个标头行中转换输出。

# Extract headers, create MultiIndex
headers = pd.read_csv('data.csv', sep=' ', header=None, nrows=2)
mi = pd.MultiIndex.from_frame(headers.T)

# Keep desired columns
dti_cols = [0, 1, 2]  # Year, Month, Day
dti_names = ['Year', 'Month', 'Day']

dat_cols = mi.get_locs([80.375]).tolist()
dat_names = mi[cols].to_flat_index().map(lambda x: f"{x[0]}_{x[1]}").tolist()

# Build dataframe
df = (pd.read_csv('data.csv', sep=' ', header=None, skiprows=2,
                  usecols=dti_cols+dat_cols, names=dti_names+dat_names,
                  parse_dates={'Date': ['Year', 'Month', 'Day']}))

输出:

>>> df
        Date  80.375_28.625  80.375_28.875  80.375_29.125  80.375_29.375
0 2015-01-01             21             22             23             24
1 2015-01-02             31             32             33             34
2 2015-01-03             41             42             43             44
3 2015-01-04             51             52             53             54

You can use two pass to extract data and headers.

# read_csv common options
opts = {'sep': ' ', 'header': None}

# Extract headers, create MultiIndex
headers = pd.read_csv('data.csv', **opts, nrows=2)
mi = pd.MultiIndex.from_frame(headers.T)

# Keep desired columns
dti = [0, 1, 2]  # Year, Month, Day
cols = mi.get_locs([80.375]).tolist()

# Build dataframe
df = pd.read_csv('data.csv', **opts, skiprows=2, index_col=dti, usecols=dti+cols)
df.columns = mi[cols]
df = df.rename_axis(index=['Year', 'Month', 'Day'], columns=['Lvl1', 'Lvl2'])
df.index = pd.to_datetime(df.index.to_frame()).rename('DateTime')

Output:

>>> df
Lvl1       80.375                     
Lvl2       28.625 28.875 29.125 29.375
DateTime                              
2015-01-01     21     22     23     24
2015-01-02     31     32     33     34
2015-01-03     41     42     43     44
2015-01-04     51     52     53     54

Input csv file:

   80.125 80.375 80.375 80.375 80.375 80.625
   28.875 28.625 28.875 29.125 29.375 28.875
2015 1 1 20 21 22 23 24 25
2015 1 2 30 31 32 33 34 35
2015 1 3 40 41 42 43 44 45
2015 1 4 50 51 52 53 54 55

Update

I need to convert the output in single header row.

# Extract headers, create MultiIndex
headers = pd.read_csv('data.csv', sep=' ', header=None, nrows=2)
mi = pd.MultiIndex.from_frame(headers.T)

# Keep desired columns
dti_cols = [0, 1, 2]  # Year, Month, Day
dti_names = ['Year', 'Month', 'Day']

dat_cols = mi.get_locs([80.375]).tolist()
dat_names = mi[cols].to_flat_index().map(lambda x: f"{x[0]}_{x[1]}").tolist()

# Build dataframe
df = (pd.read_csv('data.csv', sep=' ', header=None, skiprows=2,
                  usecols=dti_cols+dat_cols, names=dti_names+dat_names,
                  parse_dates={'Date': ['Year', 'Month', 'Day']}))

Output:

>>> df
        Date  80.375_28.625  80.375_28.875  80.375_29.125  80.375_29.375
0 2015-01-01             21             22             23             24
1 2015-01-02             31             32             33             34
2 2015-01-03             41             42             43             44
3 2015-01-04             51             52             53             54
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文