Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何排列数字在pandas中创建新列 熊猫中的示例数据集_Python_Pandas_Dataset - Fatal编程技术网

Python 如何排列数字在pandas中创建新列 熊猫中的示例数据集

Python 如何排列数字在pandas中创建新列 熊猫中的示例数据集,python,pandas,dataset,Python,Pandas,Dataset,下面是一个包含3列的数据集 c1 c2 c3 1 2 0 3 4 0 5 6 1 7 8 1 现在我想以这样一种方式排列上述数据集,即数字[3,4]和[7,8]应归入c3和c4列 c1 c2 c3 c4 c5 1 2 3 4 0 5 6 7 8 1 数据集的代码, data = {'c1':[1,2,4,6], 'c2':[2,3,5,7], 'c3':[0,0,1,1]} data = pd.DataFrame(data) 锻炼 假设有1000个这

下面是一个包含3列的数据集

c1 c2 c3
1  2   0
3  4   0
5  6   1
7  8   1
现在我想以这样一种方式排列上述数据集,即数字[3,4]和[7,8]应归入c3和c4列

c1 c2 c3 c4 c5
1  2  3  4  0
5  6  7  8  1
数据集的代码,

data = {'c1':[1,2,4,6], 'c2':[2,3,5,7], 'c3':[0,0,1,1]}
data = pd.DataFrame(data)
锻炼 假设有1000个这样的行,每行有[0或1]类。
如何在c3列和c4列中每2行排列数字

df = {'c1':[1,2,4,6,15,143,114,104,80,\
            89,100,104,70,99,70,46], 'c2':[2,3,5,7,85,80,\
            89,100,104,70,123,43,32,90,123,87], 'c3':[0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1]}

     c1   c2  c3
0     1    2   0
1     2    3   0
2     4    5   0
3     6    7   0
4    15   85   0
5   143  109   0
6   114   80   0
7   104   89   0
9    80  104   1
10   89   70   1
11  100  123   1
12  104   43   1
13   70   32   1
14   99   90   1
15   70  101   1
16   46  123   1
练习2 相应地分配课程

df = pd.DataFrame(df)
a = df[['c1','c2']].to_numpy().reshape(8, -1)

Expected output :-
        c1  c2  c3  c4  c5
         1   2   2   3  0
         4   5   6   7  0
        15  85 143  109 0
       114  80 104   89 0
       109 100  80  104 1
        89  70 100  123 1
       104  43 70   32  1
        99  90 70   101 1
练习3 增加列数

Expected output :-
        c1  c2  c3  c4  c5  c6  c7   c8  c9
         1   2   2   3  4   5   6    7    0
         
        15  85 143  109 114  80 104  89   0
       
       109 100  80  104 89  70  100  123  1
        
       104  43 70   32  99  90   70  101  1 
                   

在列表中选择要处理的列,然后通过转换为numpy数组,然后通过重塑
(2,-1)
-此处
-1
平均numpy通过数据计算创建了多少新的“列”:

a = df[['c1','c2']].to_numpy().reshape(2, -1)
print (a)
[[1 2 3 4]
 [5 6 7 8]]
最后一次传递到
DataFrame
构造函数,并使用
[0,1]
添加新列:

df = pd.DataFrame(a).assign(new=[0,1])
print (df)
   0  1  2  3  new
0  1  2  3  4    0
1  5  6  7  8    1

编辑:


假设有10k个这样的行,如果我想对每2行应用上述方法,该怎么办?假设0和1是2个类,每个类有10k个样本。@AdityaNikhil-是否可以向样本添加更多行?是的,可能。是否要我添加?
a = df[['c1','c2']].to_numpy().reshape(2, -1)
print (a)
[[  1   2   2   3   4   5   6   7  15  85 143 109 114  80 104  89 109 100]
 [ 80 104  89  70 100 123 104  43  70  32  99  90  70 101  46 123  45  87]]

df = pd.DataFrame(a).assign(new=[0,1])
print (df)
    0    1   2   3    4    5    6   7   8   9   10   11   12   13   14   15  \
0   1    2   2   3    4    5    6   7  15  85  143  109  114   80  104   89   
1  80  104  89  70  100  123  104  43  70  32   99   90   70  101   46  123   

    16   17  new  
0  109  100    0  
1   45   87    1  
df = pd.DataFrame(df)
a = df[['c1','c2']].to_numpy().reshape(8, -1)
       
df1 = pd.DataFrame(a)
df1['new'] = df['c3'].to_numpy().reshape(8, -1)[:, 0]
print (df1)
     0    1    2    3  new
0    1    2    2    3    0
1    4    5    6    7    0
2   15   85  143   80    0
3  114   89  104  100    0
4   80  104   89   70    1
5  100  123  104   43    1
6   70   32   99   90    1
7   70  123   46   87    1