如何将这个数据框转入这个数据框?

时间:2020-07-29 20:57:10

标签: python pandas

如何将df1转换为df2?

df1 = pd.DataFrame(
  {
  'item1_aspect1' : ["a", "b", "c"],
  'item1_aspect2' : [1,2,3],
  'item1_aspect3' : ["[12,34]", "[56,78]", "[99,10]"],
  'item2_aspect1' : ["a", "b", "c"],
  'item2_aspect2' : [1,2,3],
  'item2_aspect3' : ["[12,34]", "[56,78]", "[99,10]"],
  'item3_aspect1' : ["a", "b", "c"],
  'item3_aspect2' : [1,2,3],
  'item3_aspect3' : ["[12,34]", "[56,78]", "[99,10]"]
  })


df2 = pd.DataFrame({
  'aspect_1' : ["a", "b", "c", "a", "b", "c", "a", "b", "c"],
  'aspect_2' : [1,2,3,1,2,3,1,2,3],
  'aspect_3' : ["[12,34]", "[56,78]", "[99,10]", "[12,34]", "[56,78]", "[99,10]", "[12,34]", "[56,78]", "[99,10]"]
})

,即列名是一个标识符,分为多个行。我不知道该怎么做。

4 个答案:

答案 0 :(得分:1)

我们需要先调整列类型,然后再调整wide_to_long

df1.columns=df1.columns.str.split('_').map(lambda x : '_'.join(x[::-1]))

yourdf=pd.wide_to_long(df1.reset_index(),
                       ['aspect1','aspect2','aspect3'], 
                       i ='index', 
                       j = 'drop', 
                       sep = '_',suffix='\w+').reset_index(drop=True)
Out[137]: 
  aspect1  aspect2  aspect3
0       a        1  [12,34]
1       b        2  [56,78]
2       c        3  [99,10]
3       a        1  [12,34]
4       b        2  [56,78]
5       c        3  [99,10]
6       a        1  [12,34]
7       b        2  [56,78]
8       c        3  [99,10]

答案 1 :(得分:1)

如果您要坚持使用熊猫操作,就不要一直更改数据类型,而更喜欢列表推导。 试试这个方法-

lst = list(df1.columns)
n=3
new_cols = ['aspect_1', 'aspect_2', 'aspect_3']

#break the column list into groups of n = 3 in this case
chunks = [lst[i:i + n] for i in range(0, len(lst), n)]

#concatenate the list of dataframes over axis = 0after renaming columns of each 
pd.concat([df1[i].set_axis(new_cols, axis=1) for i in chunks], axis=0, ignore_index=True)
aspect_1    aspect_2    aspect_3
0   a   1   [12,34]
1   b   2   [56,78]
2   c   3   [99,10]
3   a   1   [12,34]
4   b   2   [56,78]
5   c   3   [99,10]
6   a   1   [12,34]
7   b   2   [56,78]
8   c   3   [99,10]

答案 2 :(得分:0)

获取唯一的尾列名称:

cols = df1.columns.str.split("_").str[-1].drop_duplicates()

在数据框上使用numpy的reshape,然后创建一个新的数据框:

pd.DataFrame(np.reshape(df1.to_numpy(), df1.shape[::-1]), columns=cols)

aspect1 aspect2 aspect3
0   a   1   [12,34]
1   a   1   [12,34]
2   a   1   [12,34]
3   b   2   [56,78]
4   b   2   [56,78]
5   b   2   [56,78]
6   c   3   [99,10]
7   c   3   [99,10]
8   c   3   [99,10]

或者,我们可以结合使用numpy splitnumpy vstack来获得输出:

column_count = df1.columns.str[-1].astype(int).max()

pd.DataFrame(np.vstack(np.split(df1.to_numpy(), column_count, axis=1)), columns=cols)

aspect1 aspect2 aspect3
0   a   1   [12,34]
1   b   2   [56,78]
2   c   3   [99,10]
3   a   1   [12,34]
4   b   2   [56,78]
5   c   3   [99,10]
6   a   1   [12,34]
7   b   2   [56,78]
8   c   3   [99,10]

答案 3 :(得分:0)

这是一种相当简单的方法:

df1.columns = [c[6:] for c in df1.columns]
pd.concat([df1.iloc[:, 0:3], df1.iloc[:, 3:6], df1.iloc[:, 6:9]], axis=0)

输出为:

  aspect1  aspect2  aspect3
0       a        1  [12,34]
1       b        2  [56,78]
2       c        3  [99,10]
0       a        1  [12,34]
1       b        2  [56,78]
...