简体   繁体   中英

How to add leading zeroes to Dataframe column if value is an integer, conditional on another column's values?

Is there an efficient way to add leading zeroes to a Dataframe's columns if:

  1. The column contains integer values, and
  2. For a given value of another column in the same Dataframe

Eg for the following Dataframe, how do I create a new DataFrame that has leading zeroes in values in col_2 if:

  1. col_2 are integer values (ie not "text" nor "text2" )
  2. col_1 == "A"

Initial Dataframe:

col_1   col_2
0   A   12345
1   B   863
2   A   text
3   C   893423
4   D   text2

Desired output Dataframe:

col_1   col_2
0   A   00012345
1   B   863
2   A   text
3   C   893423
4   D   text2

You can use to_numeric with errors="coerce" to ensure values are numeric:

# is the value numeric?
m1 = pd.to_numeric(df['col_2'], errors='coerce').notna()
# is col_1 equal to "A"?
m2 = df['col_1'].eq('A')

# pick the rows matching both conditions
# and do something with it
df[m1&m2]

If you want to ensure having integers and not any numerical value (ie floating points), you can use:

s = pd.to_numeric(df['col_2'], errors='coerce')
# is a numeric value and an integer
m1 = s.notna() & s.eq(s.round())

zfilling

m1 = pd.to_numeric(df['col_2'], errors='coerce').notna()
m2 = df['col_1'].eq('A')

df.loc[m1&m2, 'col_2'] = df.loc[m1&m2, 'col_2'].astype(str).str.zfill(8)

output:

  col_1     col_2
0     A  00012345
1     B       863
2     A      text
3     C    893423
4     D     text2

I think this does what you want:

df[df['col_1']=='A'].apply(lambda x: isinstance(x, int) )

Output:

col_1    False
col_2    False

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