Abhiram Satputé Abhiram Satputé - 2 months ago 10
Python Question

How to add spaces in print() after combining multiple dataframes in python?

my code looks like this:

import numpy as np
import pandas as pd

df = pd.read_csv('DelayedFlights.csv')

df["final"] =
df["Year"].map(str)+df["FlightNum"].map(str)+df["Origin"]+df["Dest"]

print(df["final"].head()) #map converts the non string data types to string


The output shows :

0 2008335IADTPA
1 20083231IADTPA
2 2008448INDBWI
3 20083920INDBWI
4 2008378INDJAX
Name: final, dtype: object


The output I want :

0 2008 335 IAD TPA
1 2008 3231 IAD TPA
2 2008 448 IND BWI
3 2008 3920 IND BWI
4 2008 378 IND JAX
Name: final, dtype: object

Answer Source

You want:

df["final"] = df["Year"].map(str)+ ' ' + df["FlightNum"].map(str)+ ' ' + df["Origin"]+ ' ' + df["Dest"]