【问题标题】:Error while splitting a column of lists into different columns将一列列表拆分为不同列时出错
【发布时间】:2020-06-02 08:05:20
【问题描述】:

我有一个名为 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)

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

编辑:这是回答者要求的我的全部代码

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

【问题讨论】:

  • 请检查amplitude_df1['amplitudes'] 中的值是否确实是列表而不是列表的字符串表示形式。例如,在#mytry 之前添加print(type(amplitude_df1['amplitudes'][1]))

标签: python pandas list numpy dataframe


【解决方案1】:

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

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

问候

【讨论】:

  • 在第一种情况下显示 numpy ndarray 没有属性 to_numpy() 的错误
  • 你能告诉我,你所说的重塑是什么意思吗?
猜你喜欢
  • 2022-12-21
  • 1970-01-01
  • 2021-10-10
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
相关资源
最近更新 更多