pandas 熊猫将 dtype 对象转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22127902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:45:27  来源:igfitidea点击:

Pandas converting dtype object to string

pythonpandas

提问by nick appel

I have trouble converting the dtype of a column. I am loading a csv file from yahoo finance.

我在转换列的 dtype 时遇到问题。我正在从雅虎财经加载一个 csv 文件。

dt = pd.read_csv('data/Tesla.csv')

this gives me the following info:

这给了我以下信息:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 923 entries, 0 to 922
Data columns (total 7 columns):
Date         923 non-null object
Open         923 non-null float64
High         923 non-null float64
Low          923 non-null float64
Close        923 non-null float64
Volume       923 non-null int64
Adj Close    923 non-null float64
dtypes: float64(5), int64(1), object(1)

i try to convert the Date into a string but whatever i try it doesn't working. I tried to loop over the row and convert it with str(). I have tried to change the dtype of the object with dt['Date'].apply(str)and I have tried a special dtype object and use that:

我尝试将日期转换为字符串,但无论我尝试什么都不起作用。我尝试遍历该行并使用 str() 对其进行转换。我试图改变对象的 dtypedt['Date'].apply(str)并且我尝试了一个特殊的 dtype 对象并使用它:

types={'Date':'str','Open':'float','High':'float','Low':'float','Close':'float','Volume':'int','Adj Close':'float'}
 dt = pd.read_csv('data/Tesla.csv', dtype=types)

But nothing seems to be working.

但似乎没有任何效果。

I use pandas version 0.13.1

我使用Pandas版本 0.13.1

采纳答案by Wesley Bowman

Converting your dates into a DateTime will allow you to easily compare a user inputted date with the dates in your data.

将日期转换为 DateTime 将允许您轻松地将用户输入的日期与数据中的日期进行比较。

#Load in the data
dt = pd.read_csv('data/Tesla.csv')

#Change the 'Date' column into DateTime
dt['Date']=pd.to_datetime(dt['Date'])

#Find a Date using strings
np.where(dt['Date']=='2014-02-28')
#returns     (array([0]),)

np.where(dt['Date']=='2014-02-21')
#returns (array([5]),)

#To get the entire row's information
index = np.where(dt['Date']=='2014-02-21')[0][0]
dt.iloc[index]

#returns:
Date         2014-02-21 00:00:00
Open                      211.64
High                      213.98
Low                       209.19
Close                      209.6
Volume                   7818800
Adj Close                  209.6
Name: 5, dtype: object

So if you wanted to do a for loop, you could create a list or numpy array of dates, then iterate through them, replacing the date in the index with your value:

因此,如果您想执行 for 循环,您可以创建一个日期列表或 numpy 数组,然后遍历它们,将索引中的日期替换为您的值:

input = np.array(['2014-02-21','2014-02-28'])
for i in input:
    index = np.where(dt['Date']==i)[0][0]
    dt.iloc[index]