简体   繁体   English

如何在新列中分隔列

[英]How can I separate a column in new columns

I don't know how to ask this question, but i'll try do explain my case.我不知道如何问这个问题,但我会尽力解释我的情况。

I have a dataset with the data as following:我有一个包含以下数据的数据集:

Product产品 Value价值 Value type值类型 year
A一种 21,5 21,5 Price价钱 21 21
A一种 5 5 Volume体积 21 21
B 55,3 55,3 Price价钱 21 21
B 10 10 Volume体积 21 21
C C 70,0 70,0 Price价钱 21 21
D D 37,5 37,5 Price价钱 21 21
D D 7,7 7,7 Volume体积 21 21

And I want to reach something like that:我想达到这样的目标:

Product产品 Price价钱 Volume体积 Year
A一种 21,5 21,5 5 5 21 21
B 55,3 55,3 10 10 21 21
c C 70,0 70,0 - —— 21 21
D D 37,0 37,0 7,7 7,7 21 21

I mind that the unstack function can solve the problem, but i don't know how, cause i'm not getting all the columns back.我认为 unstack 函数可以解决问题,但我不知道如何解决,因为我没有取回所有列。

I found a complex solution but it's not working.我找到了一个复杂的解决方案,但它不起作用。

container = []
for label, _df in df.groupby(['Year','Product']):
  _df.set_index('Value type', inplace = True)
  container.append(pd.DataFrame({
                        "Product": [label[1]],
                        "Price":[_df.loc['Price', 'Value']],
                        "Volume": [_df.loc['Volume', 'Value']],
                        "Year":[label[0]]}))

df_new = pd.concat(container)

This solution doesn't work, because the missing line for Volume for product C.此解决方案不起作用,因为产品 C 的 Volume 缺少行。

How can I reach the expected dataframe?我怎样才能达到预期的数据帧? Is there any fast way to calculate this?有没有什么快速的方法来计算这个?

Use pivot :使用pivot

out = df.pivot(index=['Product', 'year'], columns='Value type', values=['Value']) \
        .droplevel(0, axis=1).reset_index().rename_axis(None, axis=1) \
        [['Product', 'Price', 'Volume', 'year']]
>>> out
  Product  Price  Volume  year
0       A   21.5     5.0    21
1       B   55.3    10.0    21
2       C   70.0     NaN    21
3       D   37.5     7.7    21

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能让 python 识别一个词并将它分成一个新的列? - How can I make python recognize a word and separate it into a new column? 如何从 dataframe 中包含列表的列创建新列 - How can I create a new columns from a column with lists in a dataframe 如何创建一个包含两列组合的新列? - how can I create a new column with two columns combination? 如何在 Pandas 中将特定列拆分为新列? - How can I split a specific column to new columns in Pandas? 如何创建一列并根据单独的列值用新值填充它 - How to create a column and fill it with new values based on a separate columns values 如何分隔列值以创建新的不同列 - How to separate columns values to create a new different column 如何使用正则表达式拆分列以将尾随大写字母移动到单独的列中? - How can I split columns with regex to move trailing CAPS into a separate column? 如何将 Pandas dataframe 中的列分隔为唯一的箱/列? - How can I separate a column within a Pandas dataframe into unique bins / columns? 如何重构数据框以基于Column [se]值创建新的列标签,然后使用Column [value]值填充这些新列 - How can I restructure a dataframe to create new column labels based on Column[se] values and then populate those new columns with Column[value] Values 如何在python中将列分成3列 - How to separate column into 3 columns in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM