【问题标题】:pd.DataFrame problem with converting multiple arrays to columnspd.DataFrame 将多个数组转换为列的问题
【发布时间】:2021-05-31 21:40:04
【问题描述】:

我正在尝试将在 python 代码中生成的数组写入 excel。我想使用 pd.DataFrame 将数组组合到 DataFrame 并使用 df.to_excel 写入 excel 文件。但是 pd.DataFrame 适用于一个数组,但是当我提供多个数组时,它会给出奇怪的错误。例如。

fd = pd.DataFrame(globinRGBreal_BL)

这很好,但是当我尝试这样做时

fd = pd.DataFrame(globinRGBreal_BL, globoutRGBreal_BL,globinRGBreal_BL1,globoutRGBreal_BL1,globinRGBreal_BL2,globoutRGBreal_BL2)

它给出了以下错误-


TypeError                                 Traceback (most recent call last)
<ipython-input-229-ef3a5a238e44> in <module>
----> 1 fd = pd.DataFrame(globinRGBreal_BL, globoutRGBreal_BL,globinRGBreal_BL1,globoutRGBreal_BL1,globinRGBreal_BL2,globoutRGBreal_BL2)#, columns = ['4IA2-25-5-Inlet', '4IA2-25-5-Outlet', '4IA2-15-15-Inlet', '4IA2-15-15-Oulet', '4IA2-5-25-Inlet', '4IA2-5-25-Oulet' ])
      2 
      3 #fd.to_excel("output.xlsx")

TypeError: __init__() takes from 1 to 6 positional arguments but 7 were given

globinRGreal_BL 在哪里

array([0.01824094, 0.01824094, 0.0209281 , 0.02557736, 0.02839154,
       0.03028725, 0.03028725, 0.02902528, 0.02871047, 0.02557736,
       0.02591098, 0.02591098, 0.02591098, 0.02871047, 0.02997797,
       0.02997797, 0.02591098, 0.02591098, 0.02557736, 0.02871047,
       0.02871047, 0.02871047, 0.02871047, 0.02871047, 0.02591098,
       0.02523938, 0.02455002, 0.02223763, 0.01943543, 0.01943543,
       0.01943543, 0.0162949 , 0.01943543, 0.01943543, 0.01904985,
       0.01904985, 0.01943543, 0.01981595, 0.02056219, 0.0209281 ,
       0.0209281 , 0.0209281 , 0.02056219, 0.02056219, 0.02056219,
       0.02056219, 0.0201915 , 0.0201915 , 0.01275837, 0.01863662,
       0.02436383, 0.03148505, 0.03177505, 0.03666352, 0.0320614 ,
       0.02536663, 0.02536663, 0.01786171, 0.01196919, 0.01712968,
       0.01567009, 0.01749793, 0.02516712, 0.02843293, 0.03870038,
       0.02751669, 0.02369411, 0.04792953, 0.07584964, 0.09003761,
       0.07577126, 0.07075691, 0.09441191, 0.16298813, 0.25846679,
       0.30783048, 0.34328121, 0.38579438, 0.43432651, 0.46440452,
       0.48910689, 0.49488453, 0.50347702, 0.51755926, 0.51416215,
       0.50842391, 0.50292149, 0.50366578, 0.51313833, 0.52551038,
       0.54141038, 0.57929627, 0.60153184, 0.63901572, 0.65739585,
       0.66750572, 0.67295079, 0.67777958, 0.6826873 , 0.6826873 ,
       0.6826873 , 0.68645587, 0.69274747, 0.7018102 , 0.7043901 ,
       0.71765628, 0.71248386, 0.72055888, 0.73025929, 0.74441313,
       0.74441313, 0.74441313, 0.75807209, 0.75807209, 0.75551355,
       0.76413148, 0.76413148, 0.76413148, 0.76413148, 0.77285231,
       0.76490157, 0.76742507, 0.76742507, 0.78291205, 0.78291205,
       0.78785342, 0.78785342, 0.78785342, 0.80102911, 0.80102911,
       0.80102911, 0.8120127 , 0.82110077, 0.830299  , 0.830299  ,
       0.830299  , 0.8396094 , 0.8396094 , 0.8396094 , 0.8396094 ,
       0.8396094 , 0.8396094 , 0.8396094 , 0.8396094 , 0.87271125,
       0.87271125, 0.87271125, 0.87271125, 0.87271125, 0.87271125,
       0.87271125, 0.87271125, 0.87271125, 0.87271125, 0.87271125,
       0.87271125, 0.87522391, 0.87522391, 0.87522391, 0.87522391,
       0.8729085 , 0.8729085 , 0.87776768, 0.87776768, 0.87776768,
       0.87776768, 0.88757687, 0.8898683 , 0.89978377, 0.89978377,
       0.89978377, 0.90982396, 0.90982396, 0.90982396, 0.91999124,
       0.91999124, 0.90982396, 0.90982396, 0.90982396, 0.90982396,
       0.90982396, 0.90982396, 0.90982396, 0.90982396, 0.91264307,
       0.90982396, 0.90982396, 0.90982396, 0.90982396, 0.90982396,
       0.91264307, 0.91264307, 0.92806719, 0.92806719, 0.92806719,
       0.92806719, 0.92806719, 0.92806719, 0.92806719, 0.92806719,
       0.92806719, 0.92806719, 0.92806719, 0.92806719, 0.92806719,
       0.92806719, 0.92806719, 0.92806719, 0.9258321 , 0.9258321 ,
       0.93104894, 0.93104894, 0.9393637 , 0.95005631, 0.95225618,
       0.9630684 , 0.9630684 , 0.9630684 , 0.96523293, 0.95444177,
       0.95128024, 0.95128024, 0.95128024, 0.95128024, 0.94203576,
       0.94203576, 0.94203576, 0.94203576, 0.94203576, 0.94203576,
       0.94203576, 0.9339044 , 0.94203576, 0.94203576, 0.94203576,
       0.94203576, 0.94203576, 0.94203576, 0.94203576, 0.94203576,
       0.94378984, 0.94598156, 0.94598156, 0.94598156, 0.94598156,
       0.94071686, 0.94290439, 0.94290439, 0.94203576, 0.94203576,
       0.9339044 , 0.93097916, 0.93097916, 0.93097916, 0.93097916,
       0.93097916, 0.93097916, 0.93097916, 0.93097916, 0.93097916,
       0.93097916, 0.92592455, 0.92307566, 0.92307566, 0.92307566,
       0.90601619, 0.90601619, 0.90601619, 0.90601619, 0.9011152 ,
       0.9011152 , 0.9011152 , 0.88404717, 0.88153434, 0.87880679,
       0.86043462, 0.85364531, 0.85364531, 0.84693426, 0.84689513,
       0.84461875, 0.83551958, 0.82890808, 0.82448629, 0.82471838,
       0.82517075, 0.81888697, 0.81888697, 0.8100989 , 0.8100989 ,
       0.80126133, 0.79893321, 0.78581444, 0.77509769, 0.76685285,
       0.75465735, 0.73777284, 0.73381046, 0.71067965, 0.68767593,
       0.67608062, 0.64472508, 0.63116225, 0.61069817, 0.61294122,
       0.58943651, 0.57324933, 0.55132516, 0.51675782, 0.47354635,
       0.44329523, 0.41081939, 0.38044111, 0.35476907, 0.32096956,
       0.29104084, 0.27133169, 0.2475123 , 0.23119923, 0.21932964,
       0.1426785 , 0.13495904, 0.13108914, 0.11948272, 0.10751778,
       0.08484313, 0.06692096, 0.0499212 , 0.03930983, 0.03132888,
       0.03132888, 0.04495732, 0.06332789, 0.08212031, 0.0963948 ,
       0.11064034, 0.07259325, 0.07262293, 0.07262293, 0.07262293,
       0.06784511, 0.06784511, 0.06471211, 0.05673936, 0.05608275,
       0.05132892, 0.04467316, 0.04467316, 0.03976402, 0.04000123,
       0.04000123, 0.03512303, 0.04155198, 0.04155198, 0.04155198,
       0.04177698, 0.04221859, 0.04243532, 0.04243532, 0.04243532,
       0.04701616, 0.04720333, 0.0426494 , 0.0426494 , 0.04243532,
       0.0426494 , 0.04306977, 0.0434801 , 0.06530689, 0.05770976,
       0.05770976, 0.05770976, 0.05316568, 0.05330713, 0.05330713,
       0.04734221, 0.04263088, 0.04427203, 0.0414004 , 0.0416085 ,
       0.03572342, 0.03736588, 0.03736588, 0.04018885, 0.02402092,
       0.02402092, 0.02402092, 0.02402092, 0.02402092, 0.02402092,
       0.02402092, 0.02402092, 0.02371237, 0.02371237, 0.02371237,
       0.02371237, 0.02371237, 0.02371237, 0.02371237, 0.02371237,
       0.03044531, 0.03044531, 0.03044531, 0.03071937, 0.03071937,
       0.03071937, 0.03381586, 0.03381586, 0.03381586, 0.03381586,
       0.03381586, 0.03523692, 0.03523692, 0.03523692, 0.03473892,
       0.03448548, 0.03071937, 0.03071937, 0.03071937, 0.03356048,
       0.03356048, 0.03356048, 0.03356048, 0.03356048, 0.03473892,
       0.04097687, 0.04076134])

其他数组和这个类似,大小一样。

可能是什么问题?我也想给出列名但有同样的错误。

【问题讨论】:

  • 您的目标是让每个数组成为数据框中的一列吗?
  • 是的。然后用列名将其保存在 excel 中。

标签: python arrays excel pandas dataframe


【解决方案1】:

我相信您想要以下内容。只需将您的数组包装在一个列表中。您正在做的是不小心将您的数组分配给 DataFrame() 参数。

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

所以它认为 globinRGBreal_BL 是数据,globoutRGBreal_BL 是索引等等

a1 = np.array([0.01824094, 0.01824094, 0.0209281 , 0.02557736, 0.02839154,
       0.03028725, 0.03028725, 0.02902528, 0.02871047, 0.02557736])
a2 = np.array([0.02591098, 0.02591098, 0.02591098, 0.02871047, 0.02997797,
       0.02997797, 0.02591098, 0.02591098, 0.02557736, 0.02871047])
a3 = np.array([0.02871047, 0.02871047, 0.02871047, 0.02871047, 0.02591098,
       0.02523938, 0.02455002, 0.02223763, 0.01943543, 0.01943543])
a4 = np.array([0.01943543, 0.0162949 , 0.01943543, 0.01943543, 0.01904985,
       0.01904985, 0.01943543, 0.01981595, 0.02056219, 0.0209281])

df = pd.DataFrame([a1, a2, a3, a4])
df

输出

          0         1         2         3         4         5         6         7         8         9
0  0.018241  0.018241  0.020928  0.025577  0.028392  0.030287  0.030287  0.029025  0.028710  0.025577
1  0.025911  0.025911  0.025911  0.028710  0.029978  0.029978  0.025911  0.025911  0.025577  0.028710
2  0.028710  0.028710  0.028710  0.028710  0.025911  0.025239  0.024550  0.022238  0.019435  0.019435
3  0.019435  0.016295  0.019435  0.019435  0.019050  0.019050  0.019435  0.019816  0.020562  0.020928

或者如果您需要作为列

df.T

          0         1         2         3
0  0.018241  0.025911  0.028710  0.019435
1  0.018241  0.025911  0.028710  0.016295
2  0.020928  0.025911  0.028710  0.019435
3  0.025577  0.028710  0.028710  0.019435
4  0.028392  0.029978  0.025911  0.019050
5  0.030287  0.029978  0.025239  0.019050
6  0.030287  0.025911  0.024550  0.019435
7  0.029025  0.025911  0.022238  0.019816
8  0.028710  0.025577  0.019435  0.020562
9  0.025577  0.028710  0.019435  0.020928

【讨论】:

  • 感谢您的回答。在看到这个同样有效的答案之前,我使用了 xlsxwriter。我发现 pd.DataFrame 仅在所有列的数组长度相同时才有效。这对我来说是个问题,因为有些列的长度不同。 Xlsxwriter 运行良好。
  • 很高兴 xlsxwriter 工作。我也用那个。但是,不确定 pd.DataFrame 不能使用不同的数组长度是什么意思。我将 a4 阵列切成两半,它仍然有效。
  • 在我在这里提到的数组中 fd = pd.DataFrame(globinRGBreal_BL, globoutRGBreal_BL,globinRGBreal_BL1,globoutRGBreal_BL1,globinRGBreal_BL2,globoutRGBreal_BL2) 并非所有数组都具有相同的长度 - 尽管我说过 - 有一个或在一些数组中还有两个元素....如果我按照上面的方式运行代码,我会遇到奇怪的错误,但是如果我尝试逐个数组运行,那么我会得到真正的错误 ValueError: arrays must be all be same length
猜你喜欢
  • 2019-08-15
  • 2018-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-08
  • 2016-12-24
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多