Python Forum
DataFrame: To print a column value which is not null out of 5 columns
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.
Reply
#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
Reply
#3
Hello Snippsat,

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

Regards,
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add columns to polars dataframe sayyedkamran 1 1,689 Nov-03-2023, 03:01 PM
Last Post: gulshan212
  concat 3 columns of dataframe to one column flash77 2 777 Oct-03-2023, 09:29 PM
Last Post: flash77
  HTML Decoder pandas dataframe column mbrown009 3 962 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  attempt to split values from within a dataframe column mbrown009 8 2,223 Apr-10-2023, 02:06 AM
Last Post: mbrown009
  New Dataframe Column Based on Several Conditions nb1214 1 1,783 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  Putting column name to dataframe, can't work. jonah88888 1 1,803 Sep-28-2021, 07:45 PM
Last Post: deanhystad
  Setting the x-axis to a specific column in a dataframe devansing 0 1,993 May-23-2021, 12:11 AM
Last Post: devansing
  Convert several columns to int in dataframe Krayna 2 2,363 May-21-2021, 08:55 AM
Last Post: Krayna
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 2,238 May-17-2021, 07:02 AM
Last Post: lorensa74
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,268 May-09-2021, 06:46 PM
Last Post: Pit292

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020