gpt4 book ai didi

python - 为什么我会收到此错误 “ValueError: Wrong number of items passed 2, placement implies 4”,即使代码没问题?

转载 作者:太空宇宙 更新时间:2023-11-04 05:03:53 31 4
gpt4 key购买 nike

我正在使用这段代码,它给了我这个错误,ValueError: Wrong number of items passed 2, placement implies 4. 它应该在 csv 文件中打印均值、中位数和众数通过使用其他 csv 文件中的数据。当我使用此代码时,它会将平均值和中位数打印到指定的 csv 文件,但不会打印模式。这是我正在使用的代码(如下)。

import pandas as pd
df = pd.read_csv('one_hello.csv', names=['teacher', 'student',\
's.grade', 't.average','t.median','t.mode'])

df['Star Rating'] = df['Star Rating'].astype(int) #convert to integer.

df['t.mean'] = df.groupby('teacher')['s.grade'].transform('mean')
df['t.median'] = df.groupby('teacher')['s.grade'].transform('median')
df['t.mode'] = df.groupby('teacher')['s.grade'].transform(lambda group: group.mode())
df.to_csv('two_hello.csv') #write to file

文件“test_one”如下所示:

teacher         student         student grade
Jon marin 99
Jon Rob 81
Jon marly 90
Jon Ticy 90
Bon lue 76
Bon martin 76
Bon marie 56
Ton Seri 43
Ton Loku 99

我希望文件“test_two”看起来像这样:

teacher         student         s.grade    t.avg    t.median    t.mode 
Jon marin 99 90 90 90
Jon Rob 81 90 90 90
Jon marly 90 90 90 90
Jon Ticy 90 90 90 90
Bon lue 76 69.3 76 76
Bon martin 76 69.3 76 76
Bon marie 56 69.3 76 76
Ton Seri 43 69.3 71 43
Ton Loku 99 69.3 71 99

但是,它只在另一个文件上打印“平均值”和“中值”,并给我这样的错误。

`Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/pandas/core/groupby.py", line 3049, in transform
s = klass(res, indexer)
File "/usr/local/lib/python2.7/site-packages/pandas/core/series.py", line 250, in __init__
data = SingleBlockManager(data, index, fastpath=True)
File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 4117, in __init__
fastpath=True)
File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 2719, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File "/usr/local/lib/python2.7/site-packages/pandas/core/internals.py", line 115, in __init__
len(self.mgr_locs)))
ValueError: Wrong number of items passed 2, placement implies 4`

最佳答案

假设 one_hello.csvtest_one 并且 Star Ratings.grade 这应该可行。我只更改了 read_csv() 中的列名映射以匹配源文件:

import io
import pandas as pd


source = io.StringIO("""
Jon, marin, 99
Jon, Rob, 81
Jon, marly, 90
Jon, Ticy, 90
Bon, lue, 76
Bon, martin, 76
Bon, marie, 56
Ton, Seri, 43
Ton, Loku, 99
""")

df = pd.read_csv(source, names=['teacher', 'student', 's.grade'], dtype={'s.grade': int})
df['t.mean'] = df.groupby('teacher')['s.grade'].transform('mean')
df['t.median'] = df.groupby('teacher')['s.grade'].transform('median')
df['t.mode'] = df.groupby('teacher')['s.grade'].transform(lambda group: group.mode())
df.to_csv('out.csv')
print(df)

输出:

  teacher  student  s.grade     t.mean  t.median  t.mode
0 Jon marin 99 90.000000 90 90
1 Jon Rob 81 90.000000 90 90
2 Jon marly 90 90.000000 90 90
3 Jon Ticy 90 90.000000 90 90
4 Bon lue 76 69.333333 76 76
5 Bon martin 76 69.333333 76 76
6 Bon marie 56 69.333333 76 76
7 Ton Seri 43 71.000000 71 43
8 Ton Loku 99 71.000000 71 99

关于python - 为什么我会收到此错误 “ValueError: Wrong number of items passed 2, placement implies 4”,即使代码没问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44989735/

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