1

I have a dataframe that contains all sort of data -- string,int and date object too.

I already have apiece of code (deal_value(val)) that identifies the type of val and makes it a string. I need to be able to apply that to all the cells in the dataframe I have right now.

After that, I need to concatenate the row value with the row name in the dataframe.

I looked at apply function for both of these but was unable to figure out how to use it in either case


Dataframe Examples:

     name    age    dob
0    A        10    20-Jun-1969

And I want the dataframe to be:

    name         age         dob
0    A name     10 age      20-Jun-1969 dob

My function deal_value would take each cell element and make them good to concatenate into a string, so ultimately I want it to be something like:

"A name, 10 age,20-Jun-1969 AND (row-2) AND (row-3)......."
7
  • 1
    Some sample input and output would help. Please see how to create a good pandas example and provide a minimal reproducible example for your task Commented Jun 26, 2019 at 15:31
  • by row name you mean column name ? Commented Jun 26, 2019 at 15:31
  • @KaiesLAMIRI that is correct. Commented Jun 26, 2019 at 15:32
  • @piRSquared but it's not concatenating the column name yet, is it? Commented Jun 26, 2019 at 15:45
  • @user11638578 no. I missed that Commented Jun 26, 2019 at 15:46

2 Answers 2

1
import pandas

df = pandas.DataFrame({'name': 'A', 'age': 10, 'date_of_birth': '20-Jun-1969'}, index=[0])

for col in list(df.columns): 
    df[col] = df[col].apply(lambda x: ' '.join([str(col), str(x)]))


df.head()

Output

    name     age        date_of_birth
0   name A   age 10     date_of_birth 20-Jun-1969

String Output :


df_to_string = df.to_string(header=False,
                            index=False,
                            index_names=False).split('\n')
vals = [ ', '.join(element.lstrip().rstrip().split('  ')) for element in df_to_string]
vals_str = ' And '.join(vals)

print(vals_str)

Output:

'name A, age 10, date_of_birth 20-Jun-1969 And name B,  age 5, date_of_birth 21-Jun-1969'

Sign up to request clarification or add additional context in comments.

2 Comments

how would I go about concatenating it now in a string without the row number?
@user11638578 I just edited the answer for you :), happy coding
1

Seems like you just need:

df.astype(str).add(' '+df.columns)

     name     age              dob
0  A name  10 age  20-Jun-1969 dob

5 Comments

Wow this is huge, I like it but the only thing here is that if there are null values you would still stringify them
@KaiesLAMIRI if i understand the question, everything will be object dtype? since you are adding the col names anyway..?
Yes ! correct but, I mean with this solution you have less flexibility in dealing with non common cases .. but I appreciate your solution btw !!
@KaiesLAMIRI The OP hasn't specified any non-common cases to be taken care of. i would have taken a different approach then. Anyways, up-voted your's sol.
up-voted yours too ! yes I am just talking from a pythonic perspective :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.