Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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,我有一个像这样的df ip date destination_bytes source_bytes count max mean min sum count max mean min sum 0 10.0.0.1 2020-11-13 13:57:00 3 3 2.0 1 6 3 5 2.666667 1 8 1 10.0.0.1 2020-11-13 13:59:00

我有一个像这样的df

    ip  date    destination_bytes   source_bytes
                 count  max mean    min sum count   max mean    min sum
0   10.0.0.1    2020-11-13 13:57:00 3   3   2.0 1   6   3   5   2.666667    1   8
1   10.0.0.1    2020-11-13 13:59:00 0   0   0.0 0   0   0   0   0.000000    0   0
2   10.0.0.1    2020-11-13 14:01:00 1   2   2.0 2   2   1   1   1.000000    1   1
这将导致列名为元组,即
('ip',''),('date',''),('destination_bytes','sum')
如何将这些转换为连接字符串?i、 e
ip、日期、目的地\u字节\u和
条带一起使用

df.columns = df.columns.map('_'.join).str.strip('_')
或使用
f-string
s进行列表理解:

df.columns = [f'{a}_{b}'.strip('_') for a, b in df.columns]
如果有其他选择,则选择

df.columns = [f'{a}_{b}' if b != '' else a for a, b in df.columns]

非常直截了当。不过,它也在empyt的第二项
ip\uuDate\uz
上加入了他们。