【问题标题】:To append the column values of the csv追加 csv 的列值
【发布时间】:2019-12-01 15:12:20
【问题描述】:

我有一个包含 500 列的 csv。我想在另一列之后附加一列的值。

csv如下

A B  
0 3 
1 4 
2  

输出必须采用以下格式

A 
0
1
2
3
4

这意味着大量的列值将被附加在一列中。

【问题讨论】:

  • 到目前为止你做了什么?您编写的代码是否存在特定问题?你能发布你的代码并详细说明错误吗?
  • 您是否需要将 csv 中的所有列合并为一个,例如this

标签: python loops append python-3.6 python-3.5


【解决方案1】:

你可以这样做:-

#importing helping module(s)
import pandas as pd

#reading the csv data
df = pd.read_csv("path/to/csv")

#converting into numpy array
base = df.values.T

#converting to pandas series obj
base = pd.Series(base.ravel())

#filterning out nan values
base = base.dropna().values
print(base)

【讨论】:

  • 我收到一个错误 TypeError: ufunc 'isnan' not supported for the input types,并且根据转换规则''safe''@Vicrobot 无法安全地将输入强制转换为任何支持的类型
  • @arpita 我已经更新了我的答案,因此非本地 dtypes 也会被处理。
  • @Vicrobot:为什么要导入numpy
猜你喜欢
  • 2017-02-06
  • 2017-07-15
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 2021-02-16
  • 2014-11-23
  • 2016-04-16
相关资源
最近更新 更多