pandas变形

  统计/机器学习 Python    浏览次数:555        分享
0

1 3

2 4

a c

b d


变成

1 2 3 4

a b c d

就是偶数行变成列上  r*c  变成 r/2 * 2c


两种格式相互变

 

constant007   2021-10-11 12:33



   1个回答 
0

使用numpy的reshape即可:



>>> df = pd.DataFrame([[1, 3], [2,4], ['a', 'c'], ['b', 'd']])
>>> df
   0  1
0  1  3
1  2  4
2  a  c
3  b  d
>>> pd.DataFrame(df.values.reshape((2, -1)))
   0  1  2  3
0  1  3  2  4
1  a  c  b  d


SofaSofa数据科学社区DS面试题库 DS面经

threecifanggen   2021-10-14 12:04



  相关讨论

怎么对pandas dataframe中的值进行查找替换?

pandas数据列顺序不同进行concat

怎么查看pd dataframe里每一列的类型?

pandas中某列中数字后边有 亿/万这种单位 如何处理

把pandas df写入csv时UnicodeEncodeError

pandas.DataFrame的index重新排列(从0开始)

把pandas.DataFrame中所有行全部随机排列

pandas dataframe insert报错ValueError: unbounded slice

pandas数据合并 merge 相同的列能不能合并

pd.dataframe怎么同时对两个key排序?

  随便看看

pandas.DataFrame选取最后k行

牛顿法到底是一阶优化算法还是二阶优化算法?

xgboost怎么调参?

随机平均梯度法(Stochasitc Average Gradient)和随机梯度下降(SGD)有什么区别

K-means怎么选K?