Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接数据帧的多列,包括少数列中的布尔值_Python_Pandas - Fatal编程技术网

Python 连接数据帧的多列,包括少数列中的布尔值

Python 连接数据帧的多列,包括少数列中的布尔值,python,pandas,Python,Pandas,我是python新手。在我的项目中,我需要连接数据帧的多个列以创建派生列。“我的数据框”包含的列很少,只有TRUE和FALSE值。我使用下面的代码来执行连接操作 df_input["combined"] = [' '.join(row) for row in df_input[df_input.columns[0:]].values] 运行代码时出现以下错误 TypeError: sequence item 3: expected str instance, bool found 你能帮我解

我是python新手。在我的项目中,我需要连接数据帧的多个列以创建派生列。“我的数据框”包含的列很少,只有TRUE和FALSE值。我使用下面的代码来执行连接操作

df_input["combined"] = [' '.join(row) for row in df_input[df_input.columns[0:]].values]
运行代码时出现以下错误

TypeError: sequence item 3: expected str instance, bool found
你能帮我解决这个问题吗


提前感谢

让我们尝试一下
aType

df_input["combined"] = [' '.join(row.astype(str)) for row in df_input[df_input.columns[0:]].values]

您可以使用
astype(str)
强制转换
Bool
列,并使用矢量化版本连接列,如下所示

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)
脚本的输出:

my sample dataframe
   col1   col2   col3
0     1  hello   True
1     4  world  False
2     7      !   True
current columns data types
col1     int64
col2    object
col3      bool
dtype: object
combining all columns with mixed datatypes
here's how the data looks now
   col1   col2   col3       combined
0     1  hello   True   1 hello True
1     4  world  False  4 world False
2     7      !   True       7 ! True
here are the new columns datatypes
col1         int64
col2        object
col3          bool
combined    object
dtype: object
如您所见,新的
组合
包含串联数据

动态级联 要动态执行连接,请按照以下方式编辑我前面的示例:

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)

谢谢@Scott Boston&@MedAli。但我需要动态连接它。所以,我使用了Scott提到的以下代码,
df[“combined”]=[''.join(row.astype(str)),用于df[df.columns[0:].values]中的行。
。但问题是,我在组合表中得到了重复的输出。例如,当我使用MedAli的时,我输出的第一行看起来像
1hello-True 1hello-True
data@PythonLearner检查我的更新答案以处理动态连接。您是否有可以更新此问题的示例输入和预期输出。如果你跟随,你会得到更好的帮助。