我在csv文件中有以下一行,我想将所有单元格值连接成一行,然后将它们转换为字符串。
输入:
A B C D E
A hello Alan How are you
输出:你好,艾伦,你好吗
这是我现在拥有的:
df = pd.read_csv(input_file)
output = [''.join(str(df.values[:,i])) for i in range(len(df.values))]
例外:
IndexError: index 8 is out of bounds for axis 1 with size 8
任何帮助将不胜感激。
答案 0 :(得分:1)
如果只需要处理一行,则用DataFrame.iloc
或DataFrame.loc
按位置填充并添加join
:
output = ' '.join(df.iloc[0])
output = ' '.join(df.loc['A'])
print (output)
hello Alan How are you
用于处理所有行:
s = df.apply(' '.join, 1)
答案 1 :(得分:0)
您可以尝试。.
df
A B C D E
A hello Alan How are you
结果:
df[['A', 'B', 'C', 'D', 'E']].apply(lambda x: ' '.join(x), axis=1)
A hello Alan How are you
dtype: object