pandas 根据列名拆分pandas数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32739895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Splitting pandas data frame based on column name
提问by Segmented
Is there a way to split a pandas data frame based on the column name? As an example consider the data frame has the following columns df = ['A_x', 'B_x', 'C_x', 'A_y', 'B_y', 'C_y']and I want to create two data frames X = ['A_x', 'B_x', 'C_x']and Y = ['A_y', 'B_y', 'C_y'].
有没有办法根据列名拆分Pandas数据框?例如,考虑数据框具有以下列df = ['A_x', 'B_x', 'C_x', 'A_y', 'B_y', 'C_y'],我想创建两个数据框X = ['A_x', 'B_x', 'C_x']和Y = ['A_y', 'B_y', 'C_y'].
I know there is a possibility to do this:
我知道有可能这样做:
d = {'A': df.A_x, 'B': df.B_x, 'C': df.B_x}
X = pd.DataFrame (data=d)
but this would not be ideal as in my case I have 2200 columns in df. Is there a more elegant solution?
但这并不理想,因为在我的情况下,我在df. 有没有更优雅的解决方案?
回答by unutbu
You could use df.filter(regex=...):
你可以使用df.filter(regex=...):
import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(2, 10),
columns='Time A_x A_y A_z B_x B_y B_z C_x C_y C-Z'.split())
X = df.filter(regex='_x')
Y = df.filter(regex='_y')
yields
产量
In [15]: X
Out[15]:
A_x B_x C_x
0 -0.706589 1.031368 -0.950931
1 0.727826 0.879408 -0.049865
In [16]: Y
Out[16]:
A_y B_y C_y
0 -0.663647 0.635540 -0.532605
1 0.326718 0.189333 -0.803648

