如何在保留其含义的情况下将包含字符串的这些pandas列转换为float?

时间:2018-11-11 00:36:49

标签: python pandas

我想转换列“实际”,“预测”,“上一个” 为浮动,以便我可以对它们进行计算。 csv还包含一些应保留的NaN

csv文件如下:

2018-01-04 04:30:00,GBP,Low Impact Expected,Mortgage Approvals,65K,64K,65K
2018-01-04 04:51:00,EUR,Low Impact Expected,Spanish 10-y Bond Auction,1.53|1.8,,1.49|2.0
2018-01-04 05:01:00,EUR,Low Impact Expected,French 10-y Bond Auction,0.79|1.4,,0.36|1.9
2018-01-04 07:30:00,USD,Low Impact Expected,Challenger Job Cuts y/y,-3.6%,,30.1%

到目前为止,我已经尝试过:

df.columns = ['Date','Currency','Impact','Event','Actual','Forecast','Previous']

df = df[~(df['Actual'].isin('|','<']))]
#df = df[~df.Actual.str.contains("|")]

df['Actual'] = df['Actual'].str.replace('%', '')
df['Forecast'] = df['Forecast'].str.replace('%', '')
df['Previous'] = df['Previous'].str.replace('%', '')

df['Actual'] = df['Actual'].str.replace('K', '000')
df['Forecast'] = df['Forecast'].str.replace('K', '000')
df['Previous'] = df['Previous'].str.replace('K', '000')

for i in df['Actual']: float(i)
for i in df['Forecast']: float(i)
for i in df['Previous']: float(i)

摆脱的功能和<不起作用。互联网上的许多建议似乎都无法使用文件中的NaN 值。

我也无法弄清楚如何替换%,而同时移动小数点,因此数字表示正确。

希望有人可以提供帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

不是最漂亮的方法,但是我相信这是您想要的:

from io import StringIO
import pandas as pd
df = pd.read_table(StringIO("""2018-01-04 04:30:00,GBP,Low Impact Expected,Mortgage Approvals,65K,64K,65K
2018-01-04 04:51:00,EUR,Low Impact Expected,Spanish 10-y Bond Auction,1.53|1.8,,1.49|2.0
2018-01-04 05:01:00,EUR,Low Impact Expected,French 10-y Bond Auction,0.79|1.4,,0.36|1.9
2018-01-04 07:30:00,USD,Low Impact Expected,Challenger Job Cuts y/y,-3.6%,,30.1%"""), names=['Date','Currency','Impact','Event','Actual','Forecast','Previous'], sep=',')
df = df.loc[~df['Actual'].str.contains('[\|\<]')]
for col in ['Actual', 'Forecast', 'Previous']:
    df.loc[pd.notnull(df[col]) & df[col].str.contains('\%'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.contains('\%'), col].str.replace('%', '')) / 100
    df.loc[pd.notnull(df[col]) & df[col].str.endswith('K'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.endswith('K'), col].str.replace('K', '')) * 1000

答案 1 :(得分:0)

如果有人感兴趣,这是我当前的解决方案,这要感谢cosmic_inquiry的帮助。

import pandas as pd

#  Importing economic calendar
df = pd.read_csv('EconomicCalendar.csv')
df.columns = ['Date','Currency','Impact','Event','Actual','Forecast','Previous']


#  Remove no and low impact rows, remove votes beacuse of #format not convertable
df = df[df.Impact != 'Non-Economic']

event_filter = ['Asset Purchase Facility Votes', 'Official Bank Rate Votes']
df = df.loc[~df['Event'].str.contains('|'.join(event_filter))]

for col in ['Actual', 'Forecast', 'Previous']:
    #  Remove rows with certain formats not convertable
    df = df.loc[~df[col].str.contains('|'.join(['\|','\<']), na=False)]

    #  Change %, K, M, B, T into numerics
    df.loc[pd.notnull(df[col]) & df[col].str.contains('\%'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.contains('\%'), col].str.replace('%', '')) / 100
    df.loc[pd.notnull(df[col]) & df[col].str.endswith('K'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.endswith('K'), col].str.replace('K', '')) * 1000
    df.loc[pd.notnull(df[col]) & df[col].str.endswith('M'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.endswith('M'), col].str.replace('M', '')) * 1000000
    df.loc[pd.notnull(df[col]) & df[col].str.endswith('B'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.endswith('B'), col].str.replace('B', '')) * 1000000000
    df.loc[pd.notnull(df[col]) & df[col].str.endswith('T'), col] = pd.to_numeric(df.loc[pd.notnull(df[col]) & df[col].str.endswith('T'), col].str.replace('T', '')) * 1000000000000

    #  Change all to numeric to perform calculation
    df[col] = pd.to_numeric(df[col])


#  Creating Surprise column which is Actual minus Forecast
df['Surprise'] = df['Actual']-df['Forecast']