Reputation: 152
I'm trying to convert pandas dataframe to string, but I can't justify it correctly and save utf-8 values
I'm using pandas.DataFrame.to_string
function with different params (code below) but not able to achieve a desired look: table justified by left and showing utf-8 values normally.
data = [['Fire', 'das Feuer', b'\xF0\x9F\x94\xA5'],
['Air', 'der Wind', b'\xF0\x9F\x8D\x83'],
['Water', 'Wasser', b'\xF0\x9F\x8C\x8A']]
df = pd.DataFrame(data, columns=['English', 'German', 'Pic'])
string = df.to_string(columns=['German', 'Pic'], index=False, header=False, justify='left')
How it looks:
das Feuer b'\xF0\x9F\x94\xA5'
der Wind b'\xF0\x9F\x8D\x83'
Wasser b'\xF0\x9F\x8C\x8A'
How I want it to look:
das Feuer 🔥
der Wind 🍃
Wasser 🌊
I'm out of ideas. Any help would be appreciated
Upvotes: 1
Views: 1214
Reputation: 1446
loop over the index , and print the column "German" and column "Pic" . and decode column Pic" in "utf-8"
import pandas as pd
data = [['Fire', 'das Feuer', b'\xF0\x9F\x94\xA5'],
['Air', 'der Wind', b'\xF0\x9F\x8D\x83'],
['Water', 'Wasser', b'\xF0\x9F\x8C\x8A']]
df = pd.DataFrame(data, columns=['English', 'German', 'Pic'])
for i in df.index:
print(df['German'][i],df['Pic'][i].decode("utf-8"))
das Feuer 🔥
der Wind 🍃
Wasser 🌊
Upvotes: 0
Reputation: 2424
You can convert your dataframe to utf-8 before defining str
.
df.Pic = df.Pic.str.decode('utf-8')
Upvotes: 1
Reputation: 294198
decode
df = df.assign(Pic=df.Pic.str.decode('utf-8'))
s = '\n'.join(map(' '.join, zip(df.German, df.Pic)))
print(s)
das Feuer 🔥
der Wind 🍃
Wasser 🌊
Upvotes: 3