如何合并多个熊猫数据框列

时间:2019-01-07 06:37:54

标签: python pandas dataframe series

我有一个类似于下面所示的数据框。

In[2]: df = pd.DataFrame({'P1': [1, 2, None, None, None, None],'P2': [None, None, 3, 4, None, None],'P3': [None, None, None, None, 5, 6]})
Out[2]: 
    P1   P2   P3
0  1.0  NaN  NaN
1  2.0  NaN  NaN
2  NaN  3.0  NaN
3  NaN  4.0  NaN
4  NaN  NaN  5.0
5  NaN  NaN  6.0

我正在尝试将所有列合并到新数据框中的单个P列中(见下文)。

     P
0  1.0
1  2.0
2  3.0
3  4.0
4  5.0
5  6.0

在我的实际代码中,我有一个应合并的任意列列表,不一定是P1P2P3(介于1到5列之间)。我已经尝试了以下方法:

new_series = pd.Series()
desired_columns = ['P1', 'P2', 'P3']
for col in desired_columns:
    other_series=df[col]
    new_series = new_series.align(other_series)

但是,这将导致产生一个Series对象元组,并且两个对象似乎都不包含我需要的数据。我可以遍历每一行,然后检查每一列,但我觉得可能缺少一种简单的熊猫解决方案。

2 个答案:

答案 0 :(得分:5)

如果每行只有一个非None值,则向前填充None,然后按位置选择最后一列:

df['P'] = df[['P1', 'P2', 'P3']].ffill(axis=1).iloc[:, -1]
print (df)
    P1   P2   P3    P
0  1.0  NaN  NaN  1.0
1  2.0  NaN  NaN  2.0
2  NaN  3.0  NaN  3.0
3  NaN  4.0  NaN  4.0
4  NaN  NaN  5.0  5.0
5  NaN  NaN  6.0  6.0

答案 1 :(得分:0)

另一种替代解决方案:

因此,如果我们在DataFrame中不是特定于列的,则可以使用bfill()函数跨列填充数据框中的非Nan值。因此,当public partial class Form1 : Form { TileViewHitInfo tileDownHitInfo = null; public Form1() { InitializeComponent(); gridControldsdsds1.DataSource = GetTable(); treeList1.DataSource = GetTable(); tileView13333.OptionsDragDrop.AllowDrag = true; } static DataTable GetTable() { DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patient", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); table.Rows.Add(25, "Indocin", "David", DateTime.Now); table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); return table; } private void tileView1_MouseMove(object sender, MouseEventArgs e) { TileView view = sender as TileView; if (view == null) return; if (e.Button == MouseButtons.Left && tileDownHitInfo != null) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(tileDownHitInfo.HitPoint.X - dragSize.Width / 2, tileDownHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(new Point(e.X, e.Y))) { DataRow row = view.GetDataRow(tileDownHitInfo.RowHandle); view.GridControl.DoDragDrop(row, DragDropEffects.Move); tileDownHitInfo = null; DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true; } } } private void tileView1_MouseDown(object sender, MouseEventArgs e) { TileView view = sender as TileView; if (view == null) return; tileDownHitInfo = null; TileViewHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y)); if (Control.ModifierKeys != Keys.None) return; if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0) tileDownHitInfo = hitInfo; } private void treeList1_DragDroppp(object sender, DragEventArgs e) { var ext = e.Data.GetData(typeof(DataRow)); e.Effect = DragDropEffects.None; } private void treeList1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(DataRow))) e.Effect = DragDropEffects.Move; else e.Effect = DragDropEffects.None; } 时,则当前nan单元格将从同一行的下一列中显示的值填充。

axis='columns'