在python中从其他3个数据框创建一个数据框

时间:2018-08-26 11:26:26

标签: python dataframe

我正在尝试通过从其他3个(例如)数据框中获取关键信息来创建一个汇总我的关键信息的新df。

dfdate = {'x1': [2, 4, 7, 5, 6],
     'x2': [2, 2, 2, 6, 7],
     'y1': [3, 1, 4, 5, 9]}
dfdate = pd.DataFrame(df, index=range(0:4))

dfqty = {'x1': [1, 2, 6, 6, 8],
     'x2': [3, 1, 1, 7, 5],
     'y1': [2, 4, 3, 2, 8]}
dfqty = pd.DataFrame(df2, range(0:4))

dfprices = {'x1': [0, 2, 2, 4, 4],
     'x2': [2, 0, 0, 3, 4],
     'y1': [1, 3, 2, 1, 3]}
dfprices = pd.DataFrame(df3, range(0:4))

让我们说以上三个数据帧是我的数据。说一些商品的日期,数量和价格。我的新df将根据以上数据构建:

rng = len(dfprices.columns)*len(dfprices.index) # This is the len of new df
dfnew = pd.DataFrame(np.nan,index=range(0,rng),columns=['Letter', 'Number', 'date', 'qty', 'price])

现在,这是我努力拼凑东西的地方。我正在尝试将dfdate中的所有数据放入新df中的一列中。与dfqty和dfprice相同。 (因此3x5矩阵本质上是1x15向量,并放置在新的df中)。

此外,我还需要从旧df的列名中提取dfnew中的几列作为标识符。

我曾尝试过循环,但无济于事,也不知道如何将df转换为序列。但是我想要的输出是:

dfnew:
   'Lettercol','Numbercol', 'date', 'qty', 'price'
0     X            1         2       1      0
1     X            1         4       2      2
2     X            1         7       6      2
3     X            1         5       6      4
4     X            1         6       8      4      
5     X            2         2       3      2      
6     X            2         2       1      0                   
7     X            2         2       1      0                 
8     X            2         6       7      3          
9     X            2         7       5      4           
10    Y            1         3       2      1                  
11    Y            1         1       4      3           
12    Y            1         4       3      2           
13    Y            1         5       2      1          
14    Y            1         9       8      3         

其中数字0-14是索引。 字母=来自DF中col标题的字母 number =来自DF中col标题的编号 接下来的3列是orig df的数据

(不要问为什么原始数据采用这种有趣的格式:)

非常感谢。我的上一个问题不很受欢迎,所以尝试使这个问题更好,谢谢

1 个答案:

答案 0 :(得分:1)

使用:

#list of DataFrames
dfs = [dfdate, dfqty, dfprices]

#list comprehension with reshape
comb = [x.unstack() for x in dfs]
#join together
df = pd.concat(comb, axis=1, keys=['date', 'qty', 'price'])
#remove second level of MultiIndex and index to column
df = df.reset_index(level=1, drop=True).reset_index().rename(columns={'index':'col'})
#extract all values without first by indexing [1:] and first letter by [0]
df['Number'] = df['col'].str[1:]
df['Letter'] = df['col'].str[0]

cols = ['Letter', 'Number', 'date', 'qty', 'price']
#change order of columns
df = df.reindex(columns=cols)
print (df)
   Letter Number  date  qty  price
0       x      1     2    1      0
1       x      1     4    2      2
2       x      1     7    6      2
3       x      1     5    6      4
4       x      1     6    8      4
5       x      2     2    3      2
6       x      2     2    1      0
7       x      2     2    1      0
8       x      2     6    7      3
9       x      2     7    5      4
10      y      1     3    2      1
11      y      1     1    4      3
12      y      1     4    3      2
13      y      1     5    2      1
14      y      1     9    8      3