Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 DataFrame: To print a column value which is not null out of 5 columns
#1
Hello All,

I have an excel sheet with 6 columns: app_name,app_owner,bus_owner,it_lead,app_maintainer,last_updater.
Here column 2 to 6 are people names in the mentioned order.

For a given app_name, there can be names in one or more columns.

Here is a sample test data:

app_name,app_owner,bus_owner,it_lead,app_maintainer,last_updater
CRM,Tom,Hanks,Tim,Bob,Venkat
SAP,NaN,Carpenter,Velu,Karen,Lisa
eCRM,NaN,NaN,Mani,Pinky,Waren
mSales,NaN,NaN,NaN,Rosy,Lily
Mzone,NaN,NaN,NaN,NaN,Matt

I need to print the people name for app_name as input.
So, if value is present in app_owner then it should be printed, else check next column which is bus_owner for value and so on.

Expected output for the test data should be:

CRM,Tom
SAP,Carpenter
eCRM,Mani
mSales,Rosy
Mzone,Matt

I am trying to use panda's where function, but no luck.

Any help is very much appreciated.
Quote
#2
The tricky part could be solved bye df.apply(lambda x: pd.Series(x.dropna().values),1)
Can do a test,and can use to_csv or to_excel get the output.
import pandas as pd
from io import StringIO

data = StringIO("""\
app_name,app_owner,bus_owner,it_lead,app_maintainer,last_updater
CRM,Tom,Hanks,Tim,Bob,Venkat
SAP,NaN,Carpenter,Velu,Karen,Lisa
eCRM,NaN,NaN,Mani,Pinky,Waren
mSales,NaN,NaN,NaN,Rosy,Lily
Mzone,NaN,NaN,NaN,NaN,Matt""")

df = pd.read_csv(data, sep=",")

df = df.apply(lambda x: pd.Series(x.dropna().values),1)
df
	0	1	2	3	4	5
0	CRM	Tom	Hanks	Tim	Bob	Venkat
1	SAP	Carpenter	Velu	Karen	Lisa	NaN
2	eCRM	Mani	Pinky	Waren	NaN	NaN
3	mSales	Rosy	Lily	NaN	NaN	NaN
4	Mzone	Matt	NaN	NaN	NaN	NaN

first_2 = df[[0, 1]]
first_2
0	CRM	Tom
1	SAP	Carpenter
2	eCRM	Mani
3	mSales	Rosy
4	Mzone	Matt

print(first_2.to_csv(header=None, index=False))
CRM,Tom
SAP,Carpenter
eCRM,Mani
mSales,Rosy
Mzone,Matt
Quote
#3
Hello Snippsat,

Thank you very much for this solution. It worked perfect.
Appreciate your help.

Regards,
snippsat likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Assigning Column nunique values to another DataFrame column Pythonito 0 114 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Difference of two columns in Pandas dataframe zinho 2 360 Jun-17-2020, 03:36 PM
Last Post: zinho
  Issue with dataframe column nsadams87xx 0 259 May-29-2020, 02:00 AM
Last Post: nsadams87xx
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 196 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  How to print a column name in csv file Truman 1 335 Mar-31-2020, 03:34 AM
Last Post: Larz60+
  Convert dataframe string column to numeric in Python darpInd 1 380 Mar-14-2020, 10:07 AM
Last Post: ndc85430
Question Dividing a single column of dataframe into multiple columns based on char length darpInd 2 295 Mar-14-2020, 09:19 AM
Last Post: scidam
  Interate for loop over certain columns in dataframe Finpyth 2 291 Mar-06-2020, 08:34 AM
Last Post: Finpyth
  dataframe column mean skip na SriRajesh 0 207 Mar-03-2020, 01:26 PM
Last Post: SriRajesh
  How to highlight dataframe columns SriRajesh 1 283 Feb-28-2020, 11:02 PM
Last Post: Marbelous

Forum Jump:


Users browsing this thread: 1 Guest(s)