from StringIO import StringIO
import pandas as pd
st = """
col1|col2|col3
1|hello|True
4|world|False
7|!|True
"""
df = pd.read_csv(StringIO(st), sep="|")
print("my sample dataframe")
print(df.head())
print("current columns data types")
print(df.dtypes)
print("combining all columns with mixed datatypes")
df["combined"] = df["col1"].astype(str)+" "+df["col2"]+ " " +df["col3"].astype(str)
print("here's how the data looks now")
print(df.head())
print("here are the new columns datatypes")
print(df.dtypes)
脚本的输出:
^{pr2}$
如您所见,新的combined包含连接数据。在
动态连接
要动态地执行连接,下面是如何编辑我前面的示例:
from StringIO import StringIO
import pandas as pd
st = """
col1|col2|col3
1|hello|True
4|world|False
7|!|True
"""
df = pd.read_csv(StringIO(st), sep="|")
print("my sample dataframe")
print(df.head())
print("current columns data types")
print(df.dtypes)
print("combining all columns with mixed datatypes")
#df["combined"] = df["col1"].astype(str)+" "+df["col2"]+ " " +df["col3"].astype(str)
all_columns = list(df.columns)
df["combined"] = ""
for index, column_name in enumerate(all_columns):
print("current column {column_name}".format(column_name=column_name))
df["combined"] = df["combined"] + " " +df[column_name].astype(str)
print("here's how the data looks now")
print(df.head())
print("here are the new columns datatypes")
print(df.dtypes)
您可以使用
astype(str)
对Bool
列进行强制转换,并使用向量化版本将这些列连接起来,如下所示脚本的输出:
^{pr2}$如您所见,新的
combined
包含连接数据。在动态连接
要动态地执行连接,下面是如何编辑我前面的示例:
让我们试试
astype
:相关问题 更多 >
编程相关推荐