Pandas 如何将系列列表转换为两列数据帧?
我有以下系列的清单Pandas 如何将系列列表转换为两列数据帧?,pandas,list,dataframe,series,Pandas,List,Dataframe,Series,我有以下系列的清单 [LVH = 0 63 (88.73 %) LVH = 1 6 (8.45 %) LVH = 2 1 (1.41 %) LVH = 3 1 (1.41 %) dtype: object, LV diastolic dysfunction (guideline) = 0 60 (84.51 %) LV diastolic dysfunction (guideline) = 1 8 (11.27 %) LV diast
[LVH = 0 63 (88.73 %)
LVH = 1 6 (8.45 %)
LVH = 2 1 (1.41 %)
LVH = 3 1 (1.41 %)
dtype: object, LV diastolic dysfunction (guideline) = 0 60 (84.51 %)
LV diastolic dysfunction (guideline) = 1 8 (11.27 %)
LV diastolic dysfunction (guideline) = 4 3 (4.23 %)
dtype: object, LV diastolic dysfunction grade (formula) = 0.0 60 (84.51 %)
LV diastolic dysfunction grade (formula) = 1.0 4 (5.63 %)
LV diastolic dysfunction grade (formula) = 3.0 4 (5.63 %)
LV diastolic dysfunction grade (formula) = 4.0 3 (4.23 %)
dtype: object, LV filling pressure(formula) = 0 67 (94.37 %)
LV filling pressure(formula) = 1 4 (5.63 %)
dtype: object, cause of hospitalization = 8 2 (2.82 %)
cause of hospitalization = 1 43 (60.56 %)
cause of hospitalization = 2 21 (29.58 %)
cause of hospitalization = 3 1 (1.41 %)
cause of hospitalization = 6 4 (5.63 %)
dtype: object, simplfied cause of hospitalization = 1 43 (60.56 %)
simplfied cause of hospitalization = 2 22 (30.99 %)
simplfied cause of hospitalization = 3 4 (5.63 %)
simplfied cause of hospitalization = 5 2 (2.82 %)
dtype: object, ACC/AHA = A 10 (14.08 %)
ACC/AHA = 0 56 (78.87 %)
ACC/AHA = C 2 (2.82 %)
ACC/AHA = B 3 (4.23 %)
dtype: object, ACC-AHA -binary = 0 69 (97.18 %)
ACC-AHA -binary = 1 2 (2.82 %)
dtype: object, NYHA = I 65 (91.55 %)
NYHA = II 2 (2.82 %)
NYHA = III 4 (5.63 %)
dtype: object, NYHA-binary = 0 66 (92.96 %)
NYHA-binary = 1 5 (7.04 %)
dtype: object]
对于列表的每个元素,即序列,我需要将它们转换为具有两列的数据帧。例如,它应该如下所示:
Column 1 Column 2
LVH = 0 63 (88.73 %)
LVH = 1 6 (8.45 %)
LVH = 2 1 (1.41 %)
LVH = 3 1 (1.41 %)
LV diastolic dysfunction (guideline) = 0 60 (84.51 %)
LV diastolic dysfunction (guideline) = 1 8 (84.51 %)
LV diastolic dysfunction (guideline) = 4 3 (84.51 %)
...
等等。然后将转换为CSV格式供人们下载。我只使用了基本的pd.DataFrame和pd.DataFrame.from_项。第一个将其转换为dataframe,但不是我想要的方式。第二个给出了错误,但我认为这无论如何都不会有帮助。我怎样才能解决这个问题
更新
数据是一个数据框,其中包含变量的列名和度量值。数据集庞大而敏感 由于未知数据,我无法复制您的示例,因此我形成了自己的示例。你可以从这里得到帮助-
s1 = pd.Series(['1kg', '2kg'], index=['first', 'second'])
s2 = pd.Series(['3kg', '4kg'], index=['third', 'fourth'])
lst = [s1, s2]
lst
# [first 1kg
# second 2kg
# dtype: object, third 3kg
# fourth 4kg
# dtype: object]
ndf = pd.concat(lst, axis = 1, keys=[s.name for s in lst], sort=False).fillna('').apply(lambda x: ''.join(x), axis=1)
ndf = pd.DataFrame(ndf).reset_index()
ndf.columns = ['Column 1', 'Column 2']
ndf
+---+----------+----------+
| | Column 1 | Column 2 |
+---+----------+----------+
| 0 | first | 1kg |
| 1 | second | 2kg |
| 2 | third | 3kg |
| 3 | fourth | 4kg |
+---+----------+----------+
给出有问题的代码以生成series@meW,我添加了代码。定义数据too@meW你能详细说明一下吗?数据只是一个数据帧,包含287列和128000行。提供的数据也很敏感。我只对分类数据感兴趣,以了解其分布情况。还有其他几个数据集也有类似的数据。我将为每个数据框运行代码,并将其放入数据框,最终放入excel电子表格,以供用户查看每个数据集组的不同或相似之处,这些数据集组工作正常!!!!非常感谢你!!!我只需要删除sort=False,因为我得到了一个erorr声明pd.concat没有排序函数。我把它拿走了,它成功了。我会进行分类,找出为什么不起作用。
s1 = pd.Series(['1kg', '2kg'], index=['first', 'second'])
s2 = pd.Series(['3kg', '4kg'], index=['third', 'fourth'])
lst = [s1, s2]
lst
# [first 1kg
# second 2kg
# dtype: object, third 3kg
# fourth 4kg
# dtype: object]
ndf = pd.concat(lst, axis = 1, keys=[s.name for s in lst], sort=False).fillna('').apply(lambda x: ''.join(x), axis=1)
ndf = pd.DataFrame(ndf).reset_index()
ndf.columns = ['Column 1', 'Column 2']
ndf
+---+----------+----------+
| | Column 1 | Column 2 |
+---+----------+----------+
| 0 | first | 1kg |
| 1 | second | 2kg |
| 2 | third | 3kg |
| 3 | fourth | 4kg |
+---+----------+----------+