Pandas shift column data upon condition

拟墨画扇 提交于 2019-12-10 17:39:47

问题


I have dataframe which look like this.

    Name    Val Rating  
0   ABC     123 B + 
1   DEF     234 B + 
2   567     B-  NaN
3   GHI     890 D

but instead I want to shift the data by checking (col['Name']) to next column (col['Val']) and successively shifting. Also if the shifting happens change the row index value. I want the following dataframe as output.

    Name    Val Rating  
0   ABC     123 B + 
1   DEF     234 B + 
    NaN     567 B - 
2   GHI     890 D

Anybody know how to do this?


回答1:


You can shift rows by boolean mask:

mask = pd.to_numeric(df['Name'], errors='coerce').notnull()
df[mask] = df[mask].shift(axis=1)
print (df)
  Name  Val Rating
0  ABC  123    B +
1  DEF  234    B +
2  NaN  567     B-
3  GHI  890      D

Detail:

print (pd.to_numeric(df['Name'], errors='coerce'))
0      NaN
1      NaN
2    567.0
3      NaN
Name: Name, dtype: float64

If really need replace index values to empty strings is possible create helper Series and reindex.

But this is not recommended because performance problem and possible some function with this index should failed.

i = df.index[~mask]
df.index = pd.Series(range(len(i)), index=i).reindex(df.index, fill_value='')
print (df)
  Name  Val Rating
0  ABC  123    B +
1  DEF  234    B +
   NaN  567     B-
2  GHI  890      D



回答2:


df[df['Rating'].isnull()]=df[df['Rating'].isnull()].shift(axis=1)
print(df)

Output:

  Name  Val Rating
0  ABC  123    B +
1  DEF  234    B +
2  NaN  567     B-
3  GHI  890      D

Edit:

df[df['Rating'].isnull()|df['Name'].isnull()]=df[df['Rating'].isnull()|df['Name'].isnull()].shift(axis=1)
print(df)



回答3:


Using isdigit:

df[df['Name'].str.isdigit()] = df[df['Name'].str.isdigit()].shift(axis=1)

Output:

    Name  Val Rating
0  ABC  123      B +
1  DEF  234      B +
2  NaN  567      B-
3  GHI  890      D



回答4:


first define a function:

import numpy as np

def f1(row):
    if not row.rating:
         row.Rating = row.val
         row.val = row.Name
         row.Name = np.NaN

then use pandas.DataFrame.apply:

df.apply(f1,axis=1)


来源:https://stackoverflow.com/questions/51184346/pandas-shift-column-data-upon-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!