Python 熊猫:如何正确处理df2中的行=df1中的列?
我有以下数据帧Python 熊猫:如何正确处理df2中的行=df1中的列?,python,pandas,dataframe,transpose,series,Python,Pandas,Dataframe,Transpose,Series,我有以下数据帧df1: A B C D 0 case 1 1950 1.1 0 1 case 1 1951 1.3 0 2 case 1 1952 1.7 0 3 case 2 1950 1.9 0 4 case 2 1951 1.2 0 5 case 2 1952 1.4 0 我想生成一个数据帧df2,如下所示: case 1950 1951 1952 C case 1 1.1
df1
:
A B C D
0 case 1 1950 1.1 0
1 case 1 1951 1.3 0
2 case 1 1952 1.7 0
3 case 2 1950 1.9 0
4 case 2 1951 1.2 0
5 case 2 1952 1.4 0
我想生成一个数据帧df2
,如下所示:
case 1950 1951 1952
C case 1 1.1 1.3 1.7
D case 1 0 0 0
C case 2 1.9 1.2 1.4
D case 2 0 0 0
这是我的尝试:
df2=pd.DataFrame() #Empty final dataframe, "transposed"
cases=['case 1', 'case 2']
for i,s in enumerate(cases): #Iterate over scenario names
column_c=df1['C'][0+(2*i):2+(2*i)] #Identify the column C series from df1
column_c_t=column_c.transpose() #Transpose series
temp=pd.DataFrame({'Case':s}, index=['C','D']) #Empty temp dataframe
for k,j in enumerate(range(1950,1953)): #Range of years as columns
temp.insert(loc=k+1,column=str(j),value=0) #Add columns with years with initial value=0
for index, row in df1.iterrows(): #Iterate over the original dataframe
temp.loc["C":"C",1950:1952]=column_c_t
temp.loc["D":"D",1950:1952]=0
df2=df2.append(temp)
当Python返回时,这会失败
ValueError Traceback (most recent call last)
<ipython-input-66-f175a2667647> in <module>()
11
12 for index, row in ebsd3.iterrows(): #Iterate over the original dataframe
---> 13 temp.loc["C":"C",1950:1952]=column_c_t
14 temp.loc["D":"D",1950:1952]=0
15
~\AppData\Local\conda\conda\envs\my_root\lib\site-packages\pandas\core\indexing.py in __setitem__(self, key, value)
192 key = com._apply_if_callable(key, self.obj)
193 indexer = self._get_setitem_indexer(key)
--> 194 self._setitem_with_indexer(indexer, value)
195
196 def _has_valid_type(self, k, axis):
~\AppData\Local\conda\conda\envs\my_root\lib\site-packages\pandas\core\indexing.py in _setitem_with_indexer(self, indexer, value)
597
598 if len(labels) != len(value):
--> 599 raise ValueError('Must have equal len keys and value '
600 'when setting with an iterable')
601
ValueError: Must have equal len keys and value when setting with an iterable
ValueError回溯(最近一次调用)
在()
11
12对于索引,ebsd3.iterrows()中的行:#迭代原始数据帧
--->13临时位置[“C”:“C”,1950:1952]=列
14温度位置[“D”:“D”,1950:1952]=0
15
~\AppData\Local\conda\conda\envs\my\u root\lib\site packages\pandas\core\index.py in\uuuuuuuuu setitem\uuuuuuuu(self、key、value)
192 key=com.\u如果可调用,则应用(key,self.obj)
193 indexer=self.\u get\u setitem\u indexer(键)
-->194 self.\u setitem\u带索引器(索引器,值)
195
196 def_具有有效的_类型(self、k、axis):
~\AppData\Local\conda\conda\envs\my\u root\lib\site packages\pandas\core\index.py在\u setitem\u中使用\u索引器(self,indexer,value)
597
598如果透镜(标签)!=len(值):
-->599 raise value ERROR('必须具有相等的len键和值'
600'当设置为iterable'时)
601
ValueError:使用iterable设置时,必须具有相等的len键和值
我认为我做错的是将列
系列df1
分配给df2
的行c
。如有任何见解,将不胜感激。melt+pivot\u表
你应该寻找一个矢量化的解决方案。这里有一种方法可以使用pd.melt
+pd.pivot\u table
res = pd.melt(df, id_vars=['A', 'B'], value_vars=['C', 'D'])\
.pivot_table(index=['variable', 'A'], columns='B',
values=['variable'], aggfunc='sum')\
.reset_index().sort_values(['A', 'variable'])
res.columns = res.columns.droplevel()
print(res)
B 1950 1951 1952
0 C case1 1.1 1.3 1.7
2 D case1 0.0 0.0 0.0
1 C case2 1.9 1.2 1.4
3 D case2 0.0 0.0 0.0
I take
variable
是您为我手头的变量使用虚拟名称的方式。正确吗?熊猫创建一个变量
列作为melt
的一部分。你可以将每一步分开,看看发生了什么(链接的美妙之处)。