Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 熊猫从数据框中选择一组列_Python 3.x_Pandas_Dataframe - Fatal编程技术网

Python 3.x 熊猫从数据框中选择一组列

Python 3.x 熊猫从数据框中选择一组列,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,嗨,我想从一个数据帧中选择一组离散列(可能放入一个新的数据帧),然后将这些列的值转换成str,这样我就可以对它们求和并检查是否有重复的。比如说, col1 col2 col3 col4 col5 row1 s11 12 13 s14 15 row2 s12 22 23 s24 NaN row3 s13 32 NaN s34 35 s11,s12,s13是字符串,s14,s24,s34也是字符串,其他字段是数

嗨,我想从一个数据帧中选择一组离散列(可能放入一个新的数据帧),然后将这些列的值转换成
str
,这样我就可以
对它们求和
并检查是否有重复的
。比如说,

      col1  col2  col3  col4  col5
row1  s11   12    13    s14   15 
row2  s12   22    23    s24   NaN 
row3  s13   32    NaN   s34   35

s11
s12
s13
是字符串,
s14
s24
s34
也是字符串,其他字段是数字。现在我想选择列
col1
col3
col5
,并将其中的值转换为
str
,这样我就可以连接这些选定列中的值,并进行唯一性/重复性比较。最好的方法是什么

您可以使用
选择类型()
功能:

In [69]: df
Out[69]:
     col1  col2  col3 col4  col5
row1  s11    12  13.0  s14  15.0
row2  s12    22  23.0  s24   NaN
row3  s13    32   NaN  s34  35.0
row4  s12    22  23.0  s24   NaN

In [70]: (df.select_dtypes(include=['object'])
   ....:    .join(df.select_dtypes(exclude=['object']).astype(str))
   ....:    .duplicated()
   ....: )
Out[70]:
row1    False
row2    False
row3    False
row4     True
dtype: bool
说明:

In [71]: df.select_dtypes(include=['object'])
Out[71]:
     col1 col4
row1  s11  s14
row2  s12  s24
row3  s13  s34
row4  s12  s24

In [73]: df.select_dtypes(exclude=['object']).astype(str)
Out[73]:
     col2  col3  col5
row1   12  13.0  15.0
row2   22  23.0   nan
row3   32   nan  35.0
row4   22  23.0   nan
但实际上不需要连接列:

In [66]: df
Out[66]:
     col1  col2  col3 col4  col5
row1  s11    12  13.0  s14  15.0
row2  s12    22  23.0  s24   NaN
row3  s13    32   NaN  s34  35.0
row4  s12    22  23.0  s24   NaN

In [67]: df.duplicated()
Out[67]:
row1    False
row2    False
row3    False
row4     True
dtype: bool
更新:“如果我只需要col1、col3和col5,并将它们的值串联起来以进行复制,该怎么办?”

仅显示唯一性:

In [78]: df[['col1','col3','col5']].drop_duplicates()
Out[78]:
     col1  col3  col5
row1  s11  13.0  15.0
row2  s12  23.0   NaN
row3  s13   NaN  35.0

您可以使用
选择类型()
函数:

In [69]: df
Out[69]:
     col1  col2  col3 col4  col5
row1  s11    12  13.0  s14  15.0
row2  s12    22  23.0  s24   NaN
row3  s13    32   NaN  s34  35.0
row4  s12    22  23.0  s24   NaN

In [70]: (df.select_dtypes(include=['object'])
   ....:    .join(df.select_dtypes(exclude=['object']).astype(str))
   ....:    .duplicated()
   ....: )
Out[70]:
row1    False
row2    False
row3    False
row4     True
dtype: bool
说明:

In [71]: df.select_dtypes(include=['object'])
Out[71]:
     col1 col4
row1  s11  s14
row2  s12  s24
row3  s13  s34
row4  s12  s24

In [73]: df.select_dtypes(exclude=['object']).astype(str)
Out[73]:
     col2  col3  col5
row1   12  13.0  15.0
row2   22  23.0   nan
row3   32   nan  35.0
row4   22  23.0   nan
但实际上不需要连接列:

In [66]: df
Out[66]:
     col1  col2  col3 col4  col5
row1  s11    12  13.0  s14  15.0
row2  s12    22  23.0  s24   NaN
row3  s13    32   NaN  s34  35.0
row4  s12    22  23.0  s24   NaN

In [67]: df.duplicated()
Out[67]:
row1    False
row2    False
row3    False
row4     True
dtype: bool
更新:“如果我只需要col1、col3和col5,并将它们的值串联起来以进行复制,该怎么办?”

仅显示唯一性:

In [78]: df[['col1','col3','col5']].drop_duplicates()
Out[78]:
     col1  col3  col5
row1  s11  13.0  15.0
row2  s12  23.0   NaN
row3  s13   NaN  35.0

如果我只需要
col1
col3
col5
,并将它们的值串联起来用于
重复的
,@daiyue,请参阅“更新:”在我的回答中,如果我只需要
col1
col3
col5
,并将它们的值串联起来用于
重复的
,@daiyue,请参阅我的答案中的“更新”