简体   繁体   English

在pandas中切片DataFrame?

[英]Slicing a DataFrame in pandas?

I have a problem with a df in pandas. 我在熊猫中遇到df问题。 Lets suppose I have this dataframe: 让我们假设我有这个数据帧:

k = [1,2,3,4,5,6,7,8,9,10,11,12]

k = pd.DataFrame(k).T

Which is a 1x12 dataframe and I want to get a df with 4 columns of it, like k4: 这是一个1x12数据帧,我想得到一个包含4列的df,如k4:

k1 = pd.DataFrame([1,2,3,4])
k2 = pd.DataFrame( [5,6,7,8])
k3 =  pd.DataFrame([9,10,11,12])
frames = [k1,k2,k3]
k4 = pd.concat(frames, axis = 1).T

My original df is much larger than k but its number of columns is multiple of 4 and I want to slice it into a 4 columns df. 我的原始df比k大得多,但它的列数是4的倍数,我想把它切成4列df。 I guess it could be something related to i%4 == 0 but I dont really know how to do it. 我想它可能是与i%4 == 0有关的东西,但我真的不知道该怎么做。

Thanks in advance. 提前致谢。

I miss a problem. 我想念一个问题。 I should have trasposed k4. 我应该有k4。 Sorry guys. 对不起大家。

TO sum up, i have a large row with a len multiple of 4, much larger than 12: 总而言之,我有一个大行,len的倍数为4,远大于12:

    0   1   2   3   4   5   6   7   8   9   10  11
0   1   2   3   4   5   6   7   8   9  10  11  12

And I need to make a df with 4 columns, with a change of row on each 4 elements: 我需要制作一个包含4列的df,每4个元素更改一行:

  0   1   2   3
0  1   2   3   4
0  5   6   7   8
0  9  10  11  12

You can create MultiIndex in columns first by floor divide and modulo and then use stack , for remove first level of MultiIndex of index add reset_index : 您可以MultiIndex楼层除法和模数在列中创建MultiIndex ,然后使用stack ,删除index第一级MultiIndex add reset_index

k = [1,2,3,4,5,6,7,8,9,10,11,12]

k = pd.DataFrame(k).T
k.columns = [k.columns // 4, k.columns % 4]
print (k)
   0           1           2            
   0  1  2  3  0  1  2  3  0   1   2   3
0  1  2  3  4  5  6  7  8  9  10  11  12

print (k.stack().reset_index(level=0, drop=True))
   0  1   2
0  1  5   9
1  2  6  10
2  3  7  11
3  4  8  12

EDIT: 编辑:

Only need 0 for swap first level of MultiIndex , not default last level 只需要0表示MultiIndex交换第一级,而不是默认的最后一级

print (k.stack(0).reset_index(level=0, drop=True))
   0   1   2   3
0  1   2   3   4
1  5   6   7   8
2  9  10  11  12

Or swap modulo with floor dividing: 或者用地板划分交换模数:

k = [1,2,3,4,5,6,7,8,9,10,11,12]

k = pd.DataFrame(k).T
k.columns = [k.columns % 4, k.columns // 4]
print (k)
   0  1  2  3  0  1  2  3  0   1   2   3
   0  0  0  0  1  1  1  1  2   2   2   2
0  1  2  3  4  5  6  7  8  9  10  11  12

print (k.stack().reset_index(level=0, drop=True))
   0   1   2   3
0  1   2   3   4
1  5   6   7   8
2  9  10  11  12

Another numpy solution with numpy.ndarray.reshape is faster: 使用numpy.ndarray.reshape另一个numpy解决方案更快:

k = [1,2,3,4,5,6,7,8,9,10,11,12]

print (pd.DataFrame(np.array(k).reshape(-1,4)))
   0   1   2   3
0  1   2   3   4
1  5   6   7   8
2  9  10  11  12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM