Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中窄表到宽表的转换_Python_Pandas_List_Dataframe_Datatable - Fatal编程技术网

Python中窄表到宽表的转换

Python中窄表到宽表的转换,python,pandas,list,dataframe,datatable,Python,Pandas,List,Dataframe,Datatable,我在执行某些代码时遇到问题: df_transform = df_restohours.pivot_table(index='placeID', columns='days', values='hours', aggfunc='sum') 我想转换表,从 致: 我试图运行该代码,但并没有得到我所期望的表 我认为Pandas没有任何内置的方法来拆分列名,但是您可以通过循环列来实现您的目标 请尝试以下代码: import pandas as pd dd = {'placeID':[13511

我在执行某些代码时遇到问题:

df_transform = df_restohours.pivot_table(index='placeID', columns='days', values='hours', aggfunc='sum')
我想转换表,从

致:

我试图运行该代码,但并没有得到我所期望的表

我认为Pandas没有任何内置的方法来拆分列名,但是您可以通过循环列来实现您的目标

请尝试以下代码:

import pandas as pd

dd = {'placeID':[13511,13522],
      'hours':['00:00-23:22','00:00-23:33'],
      'days':['Mon;Tue;Wed;Thu;Fri','Sat;Sun']
      }

df_restohours = pd.DataFrame(dd)

df = df_restohours.pivot_table(index='placeID', columns='days', values='hours', aggfunc='sum')

print(df_restohours.to_string(index=False))

df = df.rename_axis(None,axis='columns')  # remove 'days' label
df = df.reset_index()  # move index to column

for c in df.columns:
   for c1 in c.split(';')[1:]:   # copy to new columns
       df[c1] = df[c]
df = df.rename(columns={c:c.split(';')[0] for c in df.columns})  # rename original

print(df.to_string(index=False))
输出

 placeID        hours                 days
   13511  00:00-23:22  Mon;Tue;Wed;Thu;Fri
   13522  00:00-23:33              Sat;Sun


 placeID          Mon          Sat          Tue          Wed          Thu          Fri          Sun
   13511  00:00-23:22          NaN  00:00-23:22  00:00-23:22  00:00-23:22  00:00-23:22          NaN
   13522          NaN  00:00-23:33          NaN          NaN          NaN          NaN  00:00-23:33

我认为Pandas没有任何内置的方法来拆分列名,但是通过循环遍历列可以实现您的目标

请尝试以下代码:

import pandas as pd

dd = {'placeID':[13511,13522],
      'hours':['00:00-23:22','00:00-23:33'],
      'days':['Mon;Tue;Wed;Thu;Fri','Sat;Sun']
      }

df_restohours = pd.DataFrame(dd)

df = df_restohours.pivot_table(index='placeID', columns='days', values='hours', aggfunc='sum')

print(df_restohours.to_string(index=False))

df = df.rename_axis(None,axis='columns')  # remove 'days' label
df = df.reset_index()  # move index to column

for c in df.columns:
   for c1 in c.split(';')[1:]:   # copy to new columns
       df[c1] = df[c]
df = df.rename(columns={c:c.split(';')[0] for c in df.columns})  # rename original

print(df.to_string(index=False))
输出

 placeID        hours                 days
   13511  00:00-23:22  Mon;Tue;Wed;Thu;Fri
   13522  00:00-23:33              Sat;Sun


 placeID          Mon          Sat          Tue          Wed          Thu          Fri          Sun
   13511  00:00-23:22          NaN  00:00-23:22  00:00-23:22  00:00-23:22  00:00-23:22          NaN
   13522          NaN  00:00-23:33          NaN          NaN          NaN          NaN  00:00-23:33

请在问题中提供您的代码。请参见问题中的代码。看见