I'm reading the dataframe into pandas from a csv file.
If my data looks like this:
0 1 2 3
1 0 19 12
2 5 0 13
3 6 21 0
4 7 4 15
How can I just remove the '0'?
I don't want to delete the entire row/ column just the value.
I want it to look like this:
0 1 2 3
1 5 19 12
2 6 21 13
3 7 4 15
The other solutions I found on the internet involve dropping entire columns/ rows. I don't want to lose the other data in those columns and rows. I just don't want the 0 to interfere with my min calculation.
CodePudding user response:
df=pd.DataFrame({'1':['0','5','6','7'],'2':['19','0','21','4'],'3':['12','13','0','15']})
df=df.replace('0',np.nan)
col_1=df['1'].dropna().reset_index(drop=True)
col_2=df['2'].dropna().reset_index(drop=True)
col_3=df['3'].dropna().reset_index(drop=True)
df_new=pd.concat([col_1,col_2,col_3],axis=1)
CodePudding user response:
If your string above is df_str
you can do
import io
import pandas as pd
df = df = pd.read_csv(io.StringIO(df_str), engine='python', sep='\s ', index_col=0)
df.apply(lambda x: x[x != 0].reset_index(drop=True))
and it gives you
1 2 3
0 5 19 12
1 6 21 13
2 7 4 15