I have the following dframe
and attempted code below.
import pandas as pd
dframe = pd.DataFrame({
'A' : [1,2,3],
'B' : ['Guitar', 'Piccolo', 'Sax']
})
dframe
for item in dframe:
print(f"{"Mary's favorite number is"}{dframe[0]}{"and her favorite instrument is"}{dframe[1]}")
The expected output is a formatted string for each line of the code indicating "Mary's favorite letter is 1 and her favorite instrument is Guitar", "Mary's favorite letter is 2 and her favorite instrument is Piccolo", etc.
However, the code does not seem to produce these results.
CodePudding user response:
You can use string concatenation. You have to convert column 'A' to string before:
out = "Mary's favorite number is " dframe['A'].astype(str) " and her favorite instrument is " dframe['B']
print(*out, sep='\n')
# Output
Mary's favorite number is 1 and her favorite instrument is Guitar
Mary's favorite number is 2 and her favorite instrument is Piccolo
Mary's favorite number is 3 and her favorite instrument is Sax
Update: to convert out
series to a dataframe, use to_frame
:
out = out.to_frame('colname')
print(out)
# Output
colname
0 Mary's favorite number is 1 and her favorite i...
1 Mary's favorite number is 2 and her favorite i...
2 Mary's favorite number is 3 and her favorite i...
CodePudding user response:
import pandas as pd
df = pd.DataFrame({
'A' : [1,2,3],
'B' : ['Guitar', 'Piccolo', 'Sax']
})
for index, row in df.iterrows():
print(f"Mary's favorite number is {df.A.iloc[index]} and her favorite instrument is {df.B.iloc[index]}")
output
CodePudding user response:
Here's something that works:
df["Sentence"] = df.apply(lambda row: f"Mary's favorite number is {row.A} and her favorite instrument is {row.B}", axis=1)
for sentence in df["Sentence"]:
print(sentence)
Output:
Mary's favorite number is 1 and her favorite instrument is Guitar
Mary's favorite number is 2 and her favorite instrument is Piccolo
Mary's favorite number is 3 and her favorite instrument is Sax
You could get fancier:
def sentence_maker(name, possessive_pronoun, number, instrument):
return f"{name}'s favorite number is {number} and {possessive_pronoun} favorite instrument is {instrument}"
for sentence in df.apply(lambda row: sentence_maker("Leela", "their", row.A, row.B), axis=1):
print(sentence)
Output:
Leela's favorite number is 1 and their favorite instrument is Guitar
Leela's favorite number is 2 and their favorite instrument is Piccolo
Leela's favorite number is 3 and their favorite instrument is Sax