gpt4 book ai didi

python - 将一列列表拆分为不同的列时出错

转载 作者:行者123 更新时间:2023-12-02 05:51:14 24 4
gpt4 key购买 nike

我有一个名为 amplitude_df1 的数据框,如下所示。

                                            amplitudes
0 [1.8224, 9.10515, 10.187, 4.67473, 1.60665, 1....
1 [4.40045, 15.4495, 27.3758, 17.6756, 3.21038, ...
2 [2.11535, 11.9202, 18.2254, 7.32574, 4.11506, ...
3 [3.51715, 5.90878, 14.3854, 11.5154, 8.16267, ...
4 [5.33236, 19.8225, 33.4585, 15.5712, 9.21001, ...
.. ...
196 [1.18488, 2.8276, 9.20956, 17.0281, 9.59571, 3...
197 [0.878292, 2.50281, 2.9185, 9.55309, 9.55309, ...
198 [0.220521, 0.503399, 2.16432, 2.92407, 2.92407...
199 [0.572135, 2.4478, 4.80103, 4.65729, 3.54338, ...
200 [1.14716, 1.58989, 3.63487, 6.12651, 4.42284, ...

[201 rows x 1 columns]

我试图将包含列表的幅度列拆分为不同的列,如下所示:

amplitude_df=pd.DataFrame(amplitude_df1['amplitudes'].values.tolist(),columns=list(range(len(amplitude_df1["amplitudes"][0])))

但是会弹出错误:

ValueError: Shape of passed values is (201, 1), indices imply (201, 49617)

感谢任何帮助。如有任何问题,请在评论中告诉我

编辑:这是我应 答者要求的整个代码

for filename in os.listdir(directory):
if re.search("part-00001",filename):
json_file_path = os.path.join(directory, filename)
with open(json_file_path) as f:
jsonData = json.load(f)
#print(jsonData)
if jsonData["channel"]== channel:
df = {"amplitudes":jsonData["amplitudes"],"time":jsonData["time"],
"channel":jsonData["channel"]}
df1 = pd.DataFrame(df,index=[0])
jsonNew = jsonNew.append(df1)

jsonDF = jsonNew
#sorting values with timestamp
jsonSorted = jsonDF.sort_values("time")
#resetting index after sorting
newJson = jsonSorted.reset_index(drop=True)
newJson_amp = newJson.drop(['time','channel'],axis=1)
print(newJson_amp)
amplitude_df1 = newJson_amp
#mytry
amplitude_df=pd.DataFrame(amplitude_df1['amplitudes'].values.tolist(),columns=list(range(len(amplitude_df1["amplitudes"][0])))

这就是我想要的 Pandas split column of lists into multiple columns

最佳答案

尝试使用.to_numpy()而不是.tolist()。 (您需要导入numpy)如果这不起作用,请创建一个变量并使用函数 .reshape()

reshape amplitude_df1['amplitudes'].values.to_numpy()

这是文档:https://docs.scipy.org/doc/numpy/reference/generated/numpy.reshape.html

问候

关于python - 将一列列表拆分为不同的列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60278571/

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