将多列中的所有元素合并成一个系列中的一列,同时保留NaN值。

huangapple go评论55阅读模式
英文:

Merge the all elements of multiple columns into one column in series while keeping NaNs

问题

以下是翻译好的部分:

Context: 我有5年的体重数据。第一列是日期(月份和日期),随后的列是每个月份对应的年份和体重。我想要生成一个包含所有数据的完整图表,因此我想将所有数据合并为只有两列。第一列是从2018年到2022年的日期,然后第二列是每个日期对应的体重。我已经处理了日期部分,但无法合并体重数据。实质上,我想要将...

   0    1
0  1  4.0
1  2  NaN
2  3  6.0

转换为...

   0
0  1
1  2
2  3
3  4
4  NaN
5  6.0

pd.concat 只是将年份列并排放在一起。.join.mergemeltstackagg 也都不起作用。我该如何做呢?

示例代码:

import pandas as pd
import numpy as np

df1 = pd.DataFrame({'2018': [1, 2, 3]})
df2 = pd.DataFrame({'2019': [4, np.NaN, 6]})

merged_df = pd.concat([df1, df2], axis=1, ignore_index=True, levels=0)
print(merged_df)

附言:我特别不想输入任何索引名称(例如id_vars="2018"),因为我希望这个过程可以随着年份的增加而自动化进行。

concat、merge、melt、join、stack、agg。我想将所有列数据合并为一个系列。

英文:

Context: I have 5 years of weight data. The first column is the date (month and day), the succeeding columns are the years with corresponding weight for each day of the month. I want to have a full plot of all of my data among other things and so I want to combine all into just two columns. First column is the dates from 2018 to 2022, then the second column is the corresponding weight to each date. I have managed the date part, but can't combine the weight data. In essence, I want to turn ...

   0    1
0  1  4.0
1  2  NaN
2  3  6.0

Into ...

   0
0  1
1  2
2  3
3  4
4  NaN
5  6.0

pd.concat only puts the year columns next to each other. .join, .merge, melt, stack. agg don't work either. How do I do this?

sample code:

import pandas as pd
import numpy as np

df1 = pd.DataFrame({'2018': [1, 2, 3]})
df2 = pd.DataFrame({'2019': [4, np.NaN, 6]})

merged_df = pd.concat([df1,df2],axis=1, ignore_index=True, levels = 0)
print(merged_df)

P.S. I particularly don't want to input any index names (like id_vars="2018") because I want this process to be automated as the years go by with more data.

concat, merge, melt, join, stack, agg. i want to combine all column data into just one series

答案1

得分: 0

我认为 np.ravel(merged_df, order='F') 将为您完成任务。

如果您希望以数据框的形式呈现,那么 pd.DataFrame(np.ravel(merged_df, order='F'))

英文:

I think np.ravel(merged_df,order='F') will do the job for you.

If you want it in the form of a dataframe then pd.DataFrame(np.ravel(merged_df,order='F')).

答案2

得分: 0

只需翻译代码部分,如下:

pd.concat([df["0"], df["1"].rename("0")], ignore_index=True)

输出:

0    1.0
1    2.0
2    3.0
3    4.0
4    NaN
5    6.0
Name: 0, dtype: float64
英文:

It's not fully clear what's your I/O but based on your first example, you can use concat like this :

pd.concat([df["0"], df["1"].rename("0")], ignore_index=True)

Output :

0    1.0
1    2.0
2    3.0
3    4.0
4    NaN
5    6.0
Name: 0, dtype: float64

huangapple
  • 本文由 发表于 2023年2月6日 07:11:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定