I have a data frame reading from csv using pandas.read_csv, each row of data frame looks like this:
我使用pandas.read_csv从csv读取数据帧,每行数据框如下所示:
[1, '10/18/2016 06:00', 1, 14, 0, 5.5]
Basically, it consists of integers, string, and floats. Now, I want to generate more data (newrow) based on existing data and append to the original data frame. When I try to call function append
with the following code:
基本上,它由整数,字符串和浮点数组成。现在,我想基于现有数据生成更多数据(newrow)并附加到原始数据框。当我尝试使用以下代码调用函数append时:
df.append(list(newrow))
I got the error:
我收到了错误:
RuntimeWarning: unorderable types: str() < int(), sort order is undefined for incomparable objects.
result = result.union(other)
I think the string type is playing naughty here, but I did not figure out a way to achieve this.
我认为字符串类型在这里玩顽皮,但我没有找到实现这一目标的方法。
In addition, I also tried to convert the df
to df.values
first, and then use numpy.vstack(df.values, numpy.array(newrow))
. However, the result of this code becomes
另外,我还尝试首先将df转换为df.values,然后使用numpy.vstack(df.values,numpy.array(newrow))。但是,此代码的结果变为
['1', '10/18/2016 06:00', '1', '14', '0', '5.5']
in which all fields become strings. Any help is appreciated.
其中所有字段都成为字符串。任何帮助表示赞赏。
2 个解决方案
#1
1
Will this work?
这会有用吗?
#build a DataFrame first using the list and column names from original DF.
df.append(pd.DataFrame(columns=df.columns,data=[newrow]))
#2
0
This seems to work to append to an empty Dataframes. The columns must match.
这似乎可以附加到空的Dataframes。列必须匹配。
column_titles = ['col1','col2']
data = ['1', '10/18/2016 06:00', '1', '14', '0', '5.5']
# creates empty df w/headers
df = pd.DataFrame(columns = column_titles)
df2 = pd.DataFrame([data])
df = df.append(df2)
#1
1
Will this work?
这会有用吗?
#build a DataFrame first using the list and column names from original DF.
df.append(pd.DataFrame(columns=df.columns,data=[newrow]))
#2
0
This seems to work to append to an empty Dataframes. The columns must match.
这似乎可以附加到空的Dataframes。列必须匹配。
column_titles = ['col1','col2']
data = ['1', '10/18/2016 06:00', '1', '14', '0', '5.5']
# creates empty df w/headers
df = pd.DataFrame(columns = column_titles)
df2 = pd.DataFrame([data])
df = df.append(df2)