Concatenate if there's a value - excel

I have a table with an ID column and 6 other value columns:
A B C D E F G
ID Col1 Col2 Col3 Col4 Col5 Col6
001 123 456 789
002 901 234 567 890 123 456
I'm looking for a formula that will concatenate the ID with what ever columns have values, separate by dashes (in this example).
ie.
=CONCATENATE(A2,"-",B2,"-",C2,"-",D2,"-",E2,"-",F2,"-",G2)
Only, I don't want to put dashes next to cells that don't have any value in it.
The desired output should look like this
001-123-456-789
002-901-234-567-890-123-456
With the formula I used, it looks like this:
001-123-456-789---
002-901-234-567-890-123-456

For examples :
=IF(A2<>"","-"&A2,"")&IF(B2<>"","-"&B2,"")&IF(C2<>"","-"&C2,"")&IF(D2<>"","-"&D2,"")&IF(F2<>"","-"&F2,"")&IF(G2<>"","-"&G2,"")

Related

excel find the count of 2 filtered columns

There are paired columns that I am comparing(col1 and col2, col3 and col4) with either blank or '0' or '1'. I basically want to know how many are intersect
id col1 col2 col3 col4
id1 0 1
id2 1 1 0
id3 0 1 1
id4
id5 0
for this table I want to count of how many ids are 0 or 1(between col1 and col2). If I use countA(b2:c4) I get 4 but I need to get 3 as only 3 ids are affected for each pair
. Is therea formula that would actually give 3 for col1 and col2 and 3 for col3 and col4.
SUMPRODUCT(--(B$2:B$7+C$2:C$7=0))
fails here and provides 3 instead of 5

Identify the relationship between two columns and its respective value count in pandas

I have a Data frame as below :
Col1 Col2 Col3 Col4
1 111 a Test
2 111 b Test
3 111 c Test
4 222 d Prod
5 333 e Prod
6 333 f Prod
7 444 g Test
8 555 h Prod
9 555 i Prod
Expected output :
Column 1 Column 2 Relationship Count
Col2 Col3 One-to-One 2
Col2 Col3 One-to-Many 3
Explanation :
I need to identify the relationship between Col2 & Col3 and also the value counts.
For Eg. 111(col2) is repeated 3 times and has 3 different respective values a,b,c in Col3.
This means col2 and col3 has one-to-Many relationship - count_1 : 1
222(col2) is not repeated and has only one respective value d in col3.
This means col2 and col3 has one-to-one relationshipt - count_2 : 1
333(col2) is repeated twice and has 2 different respective values e,f in col3.
This means col2 and col3 has one-to-Many relationship - count_1 : 1+1 ( increment this count for every one-to-many relationship)
Similarly for other column values increment the respective counter and display the final results as the expected dataframe.
If you only need to check the relationship between col2 and col3, you can do:
(
df.groupby(by='Col2').Col3
.apply(lambda x: 'One-to-One' if len(x)==1 else 'One-to-Many')
.to_frame('Relationship')
.groupby('Relationship').Relationship
.count().to_frame('Count').reset_index()
.assign(**{'Column 1':'Col2', 'Column 2':'Col3'})
.reindex(columns=['Column 1', 'Column 2', 'Relationship', 'Count'])
)
Output:
Column 1 Column 2 Relationship Count
0 Col2 Col3 One-to-Many 3
1 Col2 Col3 One-to-One 2

Grouping corresponding Rows based on One column

I have an Excel Sheet Dataframe with no fixed number of rows and columns.
eg.
Col1 Col2 Col3
A 1 -
A - 2
B 3 -
B - 4
C 5 -
I would like to Group Col1 which has the same content. Like the following.
Col1 Col2 Col3
A 1 2
B 3 4
C 5 -
I am using pandas GroupBy, but not getting what I wanted.
Try using groupby:
print(df.replace('-', pd.np.nan).groupby('Col1', as_index=False).first().fillna('-'))
Output:
Col1 Col2 Col3
0 A 1 2
1 B 3 4
2 C 5 -

match multiple columns within the same row

Table 1. I have a table that looks like this:
X Y Z
1 a p
2 a p
6 b p
7 c p
9 c p
Table 2. I have a different table that looks like this:
Col1 Col2 Col3 Col4
Row1 p p p
Row2 a b c
Row3 1
Row4 2
Row5 3
Row6 4
Row7 5
Row8 6
Row9 7
Row10 8
Row11 9
I want to mark "TRUE" when rows of table 1 match with values of its column in Table 1. As a result for example:
Col1 Col2 Col3 Col4
Row1 p p p
Row2 a b c
Row3 1 TRUE
Row4 2 TRUE
Row5 3
Row6 4
Row7 5
Row8 6 TRUE
Row9 7 TRUE
Row10 8
Row11 9 TRUE
Here is what I have tried so far. This is the formula for Col2 Row3:
=IFERROR(IF(AND(AND(MATCH(Col1Row3,X:X,0), MATCH(Col2Row1,Z:Z,0)), MATCH(Col2Row2,Y:Y,0)), "TRUE", ""),"")
I think it's not working because I am not containing the matches within the same row. How can I achieve my result?
Also, I do not want to specify a specific row in the formula because I have thousands of rows in Table 1, and Table 2 has to select values among those thousands of rows.
Use COUNTIFS
=IF(COUNTIFS($F:$F,$A3,$G:$G,B$2,$H:$H,B$1),TRUE,"")

Add values of a column to each other in pandas

I have a df with let's say 4 cols A,B,C,D(in real 50+ cols). I wish to add values of the cols to each other. For ex:- df['A'][0]=df['A'][0]+df['A'][1] and so on df['B'][0]... .
col1 col2 col3
A 1 222 abc
B 2 433 dfg
C 3 111 tyu
D 4 222 iop
Should become like :-
**col1** col2 col3
A 3 222 abc
B 5 433 dfg
C 7 111 tyu
D 4 222 iop
I have created a for loop to do so and after modification i am assigning back the result to the respective cols.
for k,g in colIndividual.iteritems():
if(k+1<len(colIndividual)):
print(g+colIndividual[colIndividual.index[k+1]])
However, being new to python world i don't know is it the correct and beautiful way to code this logic. This would impact the performance as in future this df might get increased to more than 50 columns. Could you please help me here?
Thanks for your kind help.
df['col1'].rolling(2).sum().shift(-1).fillna(df['col1'])
Output:
col1 col2 col3
A 3.0 222 abc
B 5.0 433 dfg
C 7.0 111 tyu
D 4.0 222 iop

Resources