这两个清单是:
lst1, lst2 = np.arange(301), 0.333 * np.arange(301)
首先,我创建了两个数据框,每个数据框有7列 .
arr1, arr2 = np.array_split(lst1, 7), np.array_split(lst2, 7)
df1 = DF(arr1).T
df2 = DF(arr2).T
这里,df1和df2各有7列 .
例如,df1具有以下列:
col0 = [0, 1, 2, 43]
col1 = [44,45,..,86]
col6 = [259,...,301]
数据帧df2也有7列 .
目标是在大型数据框架中将这7列并排放置 .
结果应如下所示:
集团集团......集团
Galaxy Diff Galaxy Diff Galaxy Diff 0
1
2
................................
43
我的尝试是这样的:
# Imports
import numpy as np
import pandas as pd
from pandas import DataFrame as DF
## Break the data
lst1, lst2 = np.arange(301), 0.333 * np.arange(301)
arr1, arr2 = np.array_split(lst1, 7), np.array_split(lst2, 7)
df1 = DF(arr1).T
df2 = DF(arr2).T
# Assign column names
clm = [ 'Group_%d'%i for i in range(len(arr1))]
df1.columns = clm
df2.columns = clm
# Make data type integer
#for i in range(df1.shape[1]):
#df1[i] = df1[i].astype(int)
df1.to_csv('tmp.txt',sep='\t')
df2.to_csv('tmp2.txt',sep='\t')
Problems
- df1中的数字是浮点数,它们应该是整数 .
- df2中的数字有很多精确度,它们应该是%.3f格式 .
df1[i] = df1[i].astype(int)
命令失败 .
4.另外,df1和df2是两个独立的数据帧,我想让它们成为一个单独的分层数据帧,其中7列各有两个子列(即gal和diff)和相应的值 .
Some related links
https://chrisalbon.com/python/pandas_hierarchical_data.html
https://pandas.pydata.org/pandas-docs/stable/advanced.html
How to apply hierarchy or multi-index to panda columns
apply hierarchy or multi-index to panda columns Pandas reset index on series to remove multiindex
1 回答
我解决了如何组合两个数据帧的相应列 .
注意:所有列应具有相同的长度(行) .
代码是:
在这里,终端打印出漂亮且格式精美的dafaframe .