我的代码如下:
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
输出显示:
^{pr2}$我想要的输出:
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
不是pandas专家,但是从docs(https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html)和您的示例中可以看出map方法可以接受一个函数,因此如果您在格式化时需要一个额外的空格,只需创建一个lambda将其转换为字符串,然后在调用时添加一个额外的空格。在
或者,使用
^{pr2}$format
你想要:
单线选项:
对于合并所有列中的文本的动态函数:
^{pr2}$或受自定义列表限制:
使用
format
:相关问题 更多 >
编程相关推荐