简体   繁体   中英

how to split one column in pandas based on value and create new columns?

I have a data frame like this:

df1 = pd.DataFrame({
               'testName':   [4402, 4402 ,5555,6753,1234,9876,3602],
               'endResult': ['WARNING', 'WARNING', 'FAILED', 'FAILED','WARNING','FAILED','WARNING'],
               })

I want to achieve this:

df = pd.DataFrame({
    'testName':[4402, 4402 ,5555,6753,1234,9876,3602],
    'WARNING':[4402,4402,0,0,1234,0,3602],
    'FAILED':[0,0,5555,6753,0,9876,0]
})

How do I do it?

Use pivot , like this:

df = (df1.reset_index()
         .pivot('index', 'endResult', 'testName')
         .fillna(0, downcast='infer')
print(df)
endResult  FAILED  WARNING
index                     
0               0     4402
1               0     4402
2            5555        0
3            6753        0
4               0     1234
5            9876        0
6               0     3602

Or, set_index using MultiIndex.from_arrays and unstack on the last level.

idx = pd.MultiIndex.from_arrays([df1.index, df1.endResult, ])
df = df1.set_index(idx).testName.unstack(fill_value=0)

print(df)
endResult  FAILED  WARNING
0               0     4402
1               0     4402
2            5555        0
3            6753        0
4               0     1234
5            9876        0
6               0     3602

Getting rid of the index while
(1) Printing

print(df.to_string(index=False))
FAILED  WARNING
     0     4402
     0     4402
  5555        0
  6753        0
     0     1234
  9876        0
     0     3602

(2) Saving to CSV

df.to_csv('data.csv', index=False)

here is how to do issue:

df1 = pd.DataFrame({
    'testName': [4402, 4402, 5555, 6753, 1234, 9876, 3602],
    'endResult': ['WARNING', 'WARNING', 'FAILED', 'FAILED', 'WARNING', 'FAILED', 
'WARNING'],
})
df = df1.where(df1["endResult"] == "FAILED").dropna()
df = df.rename(index=str, columns={"endResult": "FAILED"})
d_f = df1.where(df1["endResult"] == "WARNING").dropna()
d_f = d_f.rename(index=str, columns={"endResult": "WARNING"})
df = df.append(d_f)
df= df.fillna(0)

Solve it using unique() and where() with:

import pandas as pd
df1 = pd.DataFrame({
               'testName':   [4402, 4402 ,5555,6753,1234,9876,3602],
               'endResult': ['WARNING', 'WARNING', 'FAILED', 'FAILED','WARNING','FAILED','WARNING'],
               })


for msg in df1['endResult'].unique():
    df1[msg] = df1['testName'].where(df1['endResult']==msg,other=0)
df1.drop('endResult',axis=1,inplace=True)

print(df1)

     testName  WARNING  FAILED
0      4402     4402       0
1      4402     4402       0
2      5555        0    5555
3      6753        0    6753
4      1234     1234       0
5      9876        0    9876
6      3602     3602       0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM