gpt4 book ai didi

python - Pandas:如何正确执行 df2 中的行 = df1 中的列?

转载 作者:行者123 更新时间:2023-12-01 01:48:10 28 4
gpt4 key购买 nike

我有以下数据框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

我相信我做错的是将df1column_c_t系列分配给df2的行C 。任何见解将不胜感激。

最佳答案

融化+数据透视表

您应该寻找矢量化解决方案。这是使用 pd.melt + pd.pivot_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

关于python - Pandas:如何正确执行 df2 中的行 = df1 中的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020928/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com