熊猫有条件地连接

时间:2016-03-15 16:52:39

标签: python-3.x pandas string-concatenation

给出以下数据框:

df = pd.DataFrame({'foo':['[a]','[b'],
                   'bar':['[a','[b]']})
df
    bar  foo
0   [a]  [a
1   [b   [b]

我想添加一个结束括号"]"到那些价值观缺乏的细胞。 期望的结果是:

    bar  foo
0   [a]  [a]
1   [b]  [b]

但是,我不确定我将拥有多少列,因此我希望将其应用于整个数据框。

我从这开始,但没有运气:

df2 = df(lambda x: str(x)+"]" if (len(x)<3))

提前致谢!

更新: 我实际上正在使用一个看起来像这样的表:

    0       1        2 
0   b [r]   None    None
1   c [d    d [r]   f[d]
2   g [r]   h [d]   None
3   m [r    p [d]   None
4   b [r]   n [d
5   m [d]   a [r]   None

2 个答案:

答案 0 :(得分:2)

您可以按列循环使用,因为string函数适用于Series。将indexing with strloc

一起使用
print df
   bar  foo
0   [a  [a]
1  [b]   [b

for cols in df.columns:
    #print df[cols].str[-1] != ']'  
    df.loc[df[cols].str[-1] != ']', cols] = df[cols] + ']'
print df   
   bar  foo
0  [a]  [a]
1  [b]  [b]  

~使用带有倒置掩码的contains

for cols in df.columns:
    df.loc[~df[cols].str[-1].str.contains(']'), cols] = df[cols] + ']'
print df   
   bar  foo
0  [a]  [a]
1  [b]  [b]        

感谢您对root使用endswith的评论:

for cols in df.columns:
    df.loc[~df[cols].str.endswith(']'), cols] = df[cols] + ']'
print df 

编辑:

如果有stringsnone空值:

print df
0   [a      
1  [b]    [b
2   [a  None

for col in df.columns:
    df.loc[~df[col].str.endswith(']').replace({np.nan: False}), col] = df[col] + ']'
    df[col] = df[col].replace({']': ''})

print df   
   bar  foo
0  [a]     
1  [b]  [b]
2  [a]  NaN

答案 1 :(得分:1)

让我们了解public class LoginRegister extends AppCompatActivity implements SignUpFragment.OnFragmentInteractionListener ,SignInFragment.OnFragmentInteractionListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_register); final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_signup); tabLayout.addTab(tabLayout.newTab().setText("Sign Up")); tabLayout.addTab(tabLayout.newTab().setText("Sign in")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); final ViewPager pager =(ViewPager) findViewById(R.id.loginpager); final SigninPagerAdapter adapter = new SigninPagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount()); pager.setAdapter(adapter); tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { pager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); 函数

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {


        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}


@Override
public void onFragmentInteraction(Uri uri) {

}
}

上面一行会调用DataFrame.applymap()中每个单元格的df.applymap(func_reference) 。现在我们可以设计func_reference

df

这可能不是最有效的方式,但我认为它非常易读。

相关问题