英文:
Reshape DataFrame - Convert column value with duplicates to column header
问题
I want to change the shape of the dataframe.
我想要改变数据框的形状,将"ITEM"、"MANUFACTURER"、"MODEL"和"COST"作为列名,并将相应的值放在行中。
英文:
I have a dataframe with 2 columns. The first columns has string df[0]=[a,b,c]*5.My second column has corresponding values[cat,dog,4...etc].I want to change the shape of dataframe.
0 1
0 ITEM cat
1 MANUFACTURER queen
2 MODEL black
3 COST 5
4 ITEM dog
5 MANUFACTURER god
6 MODEL All-in-Wonder White
7 COST 50
I want Item Manufacturer Model and Cost as column Names and corresponding values in rows.
答案1
得分: 1
It sounds like you want a pandas.pivot operation.
For your data, it might look like this:
import pandas as pd
data = pd.DataFrame({
0: 2 * ["ITEM", "MANUFACTURER", "MODEL", "COST"],
1: ["cat", "queen", "black", 5, "dog", "god", "All-in-Wonder White", 50]
})
# Encode how rows should be grouped
data['index'] = [1, 1, 1, 1, 2, 2, 2, 2]
data.pivot(values=1, columns=0, index='index')
0 COST ITEM MANUFACTURER MODEL
index
1 5 cat queen black
2 50 dog god All-in-Wonder White
英文:
It sounds like you want a pandas.pivot operation.
For your data, it might look like this:
import pandas as pd
data = pd.DataFrame({
0: 2 * ["ITEM", "MANUFACTURER", "MODEL", "COST"],
1: ["cat", "queen", "black", 5, "dog", "god", "All-in-Wonder White", 50]
})
# Encode how rows should be grouped
data['index'] = [1,1,1,1,2,2,2,2]
data.pivot(values=1, columns=0, index='index')
0 COST ITEM MANUFACTURER MODEL
index
1 5 cat queen black
2 50 dog god All-in-Wonder White
答案2
得分: 0
import pandas as pd
data = pd.DataFrame({
0: 2 * ["项目", "制造商", "型号", "成本"],
1: ["猫", "女王", "黑色", 5, "狗", "上帝", "全能白", 50]
})
transformed_data = pd.DataFrame(
dict(data
.groupby(0)
.agg(lambda x: tuple(x))
.reset_index()
.to_dict(orient='split')['data'])
)
英文:
import pandas as pd
data = pd.DataFrame({
0: 2 * ["ITEM", "MANUFACTURER", "MODEL", "COST"],
1: ["cat", "queen", "black", 5, "dog", "god", "All-in-Wonder White", 50]
})
transformed_data = pd.DataFrame(
dict(data
.groupby(0)
.agg(lambda x: tuple(x))
.reset_index()
.to_dict(orient='split')['data'])
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论