创建单行python pandas数据框 [英] Create single row python pandas dataframe

查看:89
本文介绍了创建单行python pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一行创建一个python pandas DataFrame,以使用更多的pandas功能,例如转储到* .csv.

I want to create a python pandas DataFrame with a single row, to use further pandas functionality like dumping to *.csv.

我已经看到了类似以下代码的代码,但最终只能使用列结构,但数据为空

I have seen code like the following being used, but I only end up with the column structure, but empty data

import pandas as pd

df = pd.DataFrame()
df['A'] = 1
df['B'] = 1.23
df['C'] = "Hello"
df.columns = [['A','B','C']]

print df

Empty DataFrame
Columns: [A, B, C]
Index: []

虽然我知道还有其他方法(例如从字典中),但我想了解为什么这段代码对我不起作用!?这是版本问题吗? (使用pandas == 0.19.2)

While I know there are other ways to do it (like from a dictionary), I want to understand why this piece of code is not working for me!? Is this a version issue? (using pandas==0.19.2)

推荐答案

In [399]: df = pd.DataFrame(columns=list('ABC'))

In [400]: df.loc[0] = [1,1.23,'Hello']

In [401]: df
Out[401]:
   A     B      C
0  1  1.23  Hello

或:

In [395]: df = pd.DataFrame([[1,1.23,'Hello']], columns=list('ABC'))

In [396]: df
Out[396]:
   A     B      C
0  1  1.23  Hello

这篇关于创建单行python pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆