我如何在Python中使用Pandas 制作这个表格:

Data=[[‘4/04/2023’,‘Floor’,‘Tables’,‘Roof’,‘Paint’,‘],[’4/05/2023‘,’‘,’Floor‘,’Tables‘,’Roof‘,’Paint‘],[’4/06/2023‘,’Paint‘,’Floor‘,’Tables‘,’Roof‘,’‘,‘Floors’,‘Tables’]]

Df=pd.DataFrame(数据,列=[‘Date’,‘Jim’,‘Bob’,‘Ed’,‘James’,‘Joe’])

enter image description here

Look like this? I think I have to use melt and Pivot, but I can't figure out how to get it to work. enter image description here

推荐答案

我命令你这么做的,go 查清楚:

#let's create some initial data
data = [['4/04/2023','Floors','Tables','Roof','Paint',''],['4/05/2023','','Floors','Tables','Roof','Paint'],['4/06/2023','Paint','','Floors','Tables','Roof'],['4/07/2023','Roof','Paint','','Floors','Tables']]
df = pd.DataFrame(data, columns=['Date', 'Jim','Bob','Ed','James','Joe'])

#apply the transformations
df2 = df.melt(id_vars="Date", value_name='Items')
df3 = df2.pivot(index="Date", columns='Items', values='variable')
df3 = df3[["Floors","Tables","Roof", "Paint"]] #change the order

As a result I got this:
Result

Python相关问答推荐

错误:找不到TensorFlow / Cygwin的匹配分布

为什么判断pd.DataFrame的值与判断pd.Series的值存在差异(如果索引中有值)?

将大小为n*512的数组绘制到另一个大小为n*256的数组的PC组件

使用decorator 重复超载

请从Python访问kivy子部件的功能需要帮助

根据给定日期的状态过滤查询集

Class_weight参数不影响RandomForestClassifier不平衡数据集中的结果

如何在图片中找到这个化学测试条?OpenCV精明边缘检测不会绘制边界框

在Python中对分层父/子列表进行排序

使可滚动框架在tkinter环境中看起来自然

如何使用LangChain和AzureOpenAI在Python中解决AttribeHelp和BadPressMessage错误?

log 1 p numpy的意外行为

计算组中唯一值的数量

如果值发生变化,则列上的极性累积和

从spaCy的句子中提取日期

我的字符串搜索算法的平均时间复杂度和最坏时间复杂度是多少?

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

Python—转换日期:价目表到新行

跳过嵌套JSON中的级别并转换为Pandas Rame

在方法中设置属性值时,如何处理语句不可达[Unreacable]";的问题?