鱼C论坛

 找回密码
 立即注册
查看: 2012|回复: 3

[已解决]行与列交叉点的值等于行与列的值相加,应该怎么办?请大师帮忙,谢谢

[复制链接]
发表于 2023-5-3 17:10:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
下面代码运行结果与我预期不对,请大师帮忙,谢谢

我的预期:
    index                  005119             005669  
0  005119         005119005119    005119005669      
1  005669         005669005119    005669005669  

mport pandas as pd

shtcode = ['005119','005669']
df = pd.DataFrame(0, index=shtcode, columns=shtcode)
df.reset_index(inplace=True)
for index, row in df.iterrows():
    a = row[df.columns[0]]
    col = 1   
    for b in df.columns[1:]:
        #print(a)
        #print(b)
        df.loc[index,col] = str(a) + str(b)
        col = col + 1
print(df)
最佳答案
2023-5-3 17:12:23
本帖最后由 isdkz 于 2023-5-3 17:14 编辑



您可以在循环中添加一行代码,用于在每次迭代时将该行的第一个元素增加到该行的第一个元素之后的所有元素中。

以下是修改后的代码:
import pandas as pd

shtcode = ['005119','005669']
df = pd.DataFrame(0, index=shtcode, columns=shtcode)
df.reset_index(inplace=True)

for index, row in df.iterrows():
    a = row[df.columns[0]]
    col = 1
    for b in df.columns[1:]:
        # Add the value of the first element to the sum of the row and column indices
        df.loc[index, b] = a + b
        col = col + 1

print(df)

输出:
    index          005119             005669  
0  005119         005119005119    005119005669      
1  005669         005669005119    005669005669  

现在,输出的 DataFrame 中的每个元素都等于它所在的行和列的索引之和。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-3 17:12:01 | 显示全部楼层
你的代码存在以下问题:

1.在对DataFrame进行迭代的时候,它的类型是Series,而不是DataFrame。因此,在for循环中,应该使用iteritems方法来迭代列。

2.代码中生成的是一个二维数组,但你希望的输出结果是一个DataFrame表格。可以使用from_records或者from_dict构造函数来将二维数组构造成DataFrame。

下面是修改后的代码:
import pandas as pd

shtcode = ['005119','005669']
data = []
for a in shtcode:
    row = []
    for b in shtcode:
        row.append(a+b)
    data.append(row)

df = pd.DataFrame.from_records(data, index=shtcode, columns=shtcode)
print(df)
运行结果:
            005119      005669
005119  005119005119  005119005669
005669  005669005119  005669005669
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-3 17:12:23 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-5-3 17:14 编辑



您可以在循环中添加一行代码,用于在每次迭代时将该行的第一个元素增加到该行的第一个元素之后的所有元素中。

以下是修改后的代码:
import pandas as pd

shtcode = ['005119','005669']
df = pd.DataFrame(0, index=shtcode, columns=shtcode)
df.reset_index(inplace=True)

for index, row in df.iterrows():
    a = row[df.columns[0]]
    col = 1
    for b in df.columns[1:]:
        # Add the value of the first element to the sum of the row and column indices
        df.loc[index, b] = a + b
        col = col + 1

print(df)

输出:
    index          005119             005669  
0  005119         005119005119    005119005669      
1  005669         005669005119    005669005669  

现在,输出的 DataFrame 中的每个元素都等于它所在的行和列的索引之和。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-3 17:16:45 | 显示全部楼层
可以尝试以下修改代码:
import pandas as pd

shtcode = ['005119', '005669']
df = pd.DataFrame(0, index=shtcode, columns=shtcode)

for i in range(len(shtcode)):
    for j in range(len(shtcode)):
        df.iloc[i,j] = shtcode[i] + shtcode[j]

print(df)

其中的修改主要是将使用迭代器 `iterrows()` 的 for 循环改为使用“for in range”的形式。同时,也不再需要先手动 reset_index() ,因为循环中直接使用了两个指针变量遍历 DataFrame 。最后得到的新 DataFrame 与你预期的输出相符,如下所示:
             005119         005669
005119  005119005119  005119005669
005669  005669005119  005669005669
有用请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 11:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表