在python中重塑数据框的列值

时间:2018-07-30 21:50:09

标签: python pandas dataframe

我有一个如下所示的Dataframe,没有标题。

当前df:

Col 0    Col 1   Col 2   Col3  
2345      abcd  mobile    oneplus
4567      abbb  internet  explorer 
mozilla   2345  cccc      dddd 
eeee      bbbb  1234      hello

我希望将数字值(即ID)作为第一列(列索引0)。

如果在任何行中,数字值都移至Col 1,将Col 1Col2值合并并放入Col 1,然后放入Col3值转换为Col2,然后将下一行的Col0值作为上一行的Col3

预期输出如下:

Col 0     Col 1          Col 2    Col3  
2345      abcd           mobile    oneplus 
4567      abbbinternet   explorer  mozilla
2345      ccccdddd        eeee     bbbb    
1234      hello  

帮助非常感谢!谢谢..

2 个答案:

答案 0 :(得分:1)

您可以使用stackset_indexunstack来做到这一点:

from io import StringIO

txt = StringIO("""2345      abcd  mobile    oneplus
4567      abbb  internet  explorer 
mozilla   2345  cccc      dddd 
eeee      bbbb  1234      hello""")

df = pd.read_csv(txt, header=None, sep='\s+')

df = df.stack().reset_index(drop=True)

df1 = df.to_frame().set_index(df.str.isnumeric().cumsum())

df_out = df1.set_index(df1.groupby(level=0).cumcount(), append=True)[0].unstack()
df_out

输出:

      0      1         2         3        4
1  2345   abcd    mobile   oneplus      NaN
2  4567   abbb  internet  explorer  mozilla
3  2345   cccc      dddd      eeee     bbbb
4  1234  hello       NaN       NaN      NaN

答案 1 :(得分:1)

在将此数据读入熊猫之前,进行清洗可能会更容易。假设您的数据是CSV,而不是以往最漂亮的代码,但这应该可以做到:

import numpy as np
import pandas as pd
import re

filename = "<path to file>.csv"
new_file = "<path to where fixed csv should go>.csv"

with open(filename, "r") as infile:
    text = infile.read()

# get rid of existing new line characters
text = text.replace("\n", ",")

# put a new line before every number
out = re.sub("([0-9]+)", "\n\\1", text)

# write out
with open(new_file, "w+") as outfile:
    outfile.write(out)

# read in the fixed csv -- need to provide a number of columns
# greater than you'll need (using 50 here), and then cut the excess
df = pd.read_csv(new_file, header=None, names=range(50)).dropna(how="all", axis=1)

# jam as many columns into column1 as necessary to get just 3 after ID
df["cols_to_jam"] = df[df.columns[1:]].notnull().sum(axis=1) - 3

def jam(row):
     if row["cols_to_jam"] > 0:
         new = ""
         for col in range(1, row["cols_to_jam"] + 2):
             new += str(row[col])
     else:
         new = row[1]
     return new

idx = df[0]
col1 = df.apply(jam, axis=1)

# blank out jammed values
for i, row in df.iterrows():
    if row["cols_to_jam"] > 0:
        for col in range(1, row["cols_to_jam"] + 2):
            df.ix[i, col] = np.nan
    else:
        df.ix[i, 1] = np.nan

del df["cols_to_jam"], df[0]

remaining_cols = df.apply(lambda x: list(x.dropna().tail(2).values), axis=1).apply(pd.Series)
remaining_cols.columns = ["col2", "col3"]

# put it all together
output = idx.to_frame("id").join(col1.to_frame("col1")).join(remaining_cols)