简体   繁体   中英

Move certain pandas dataframe column values from one column to another and replace old position with Nan

I have a dataframe like this:

data = {"Name": ["Jason", "Jason", "Jason", "Jason", "Pat", "Amy", "Amy"]}
df = pd.DataFrame(data)

    Name
0  Jason
1  Jason
2  Jason
3  Jason
4    Pat
5    Amy
6    Amy

and I need it to look like this:

    Name Name2 Name3
0  Jason   Nan   Nan
1  Jason   Nan   Nan
2  Jason   Nan   Nan
3  Jason   Nan   Nan
4    Nan   Pat   Nan
5    Nan   Nan   Amy
6    Nan   Nan   Amy

I can manually create something in the direction I want to go but not sure how to automatically create the new columns by the count of unique values found in the "Name" column. I also need the values in the new column to be on the same row index. I found that the list always changes too, so using 'unique_names[0]' won't always work. Here's what I have tried so far but stuck. Also, this is just an example for one column but this would actually have about 17 similar columns with different values. Thanks

unique_names = list(set([p for p in df["Name"]]))
# ['Pat', 'Jason', 'Amy']

count = len(unique_names)    # Trying to fit this somewhere to give it a count to refer to
# 3

for item in df["name"]:
if unique_names[0] == item:
    df["new_name"] = pd.Series(item)


    Name New_name
0  Jason      Pat
1  Jason      NaN
2  Jason      NaN
3  Jason      NaN
4    Pat      NaN
5    Amy      NaN
6    Amy      NaN

We can do str.get_dummies then mul

s=df.Name.str.get_dummies().mul(df.Name,axis=0).replace('',np.nan)
s
Out[54]: 
   Amy  Jason  Pat
0  NaN  Jason  NaN
1  NaN  Jason  NaN
2  NaN  Jason  NaN
3  NaN  Jason  NaN
4  NaN    NaN  Pat
5  Amy    NaN  NaN
6  Amy    NaN  NaN

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