Skip to main content
 首页 » 编程设计

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

2024年12月31日10xing901022

我正在尝试创建一个新的 df 来总结我的关键信息,方法是从 3 个(比如)其他数据框中获取该信息。

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)) 

假设以上 3 个数据框是我的数据。比如说,商品的一些日期、数量和价格。我的新 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 中)。

除此之外,我还需要 dfnew 中的几列作为标识符,来自旧 df 的列名。

我试过 for 循环但无济于事,并且不知道如何将 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 是索引。 letter = 来自 DF 中 col 标题的字母 number = 来自 DF 中 col header 的数字 接下来的 3 列是来自 orig df 的数据

(不要问为什么原始数据是那种有趣的格式:)

非常感谢。我的上一个问题没有得到很好的接受,所以我试图让这个问题变得更好,谢谢

请您参考如下方法:

使用:

#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