如果所有列均已分配对象,如何区分熊猫中的列数据类型

时间:2019-03-14 19:41:30

标签: python pandas dataframe

我正在导入具有5列数据(不同数据类型)的文本文件。由于某种原因,一旦导入并清除了数据。它们在熊猫中都被分配为对象类型,因此无法区分列。

我的目标是按数据类型区分列,并删除包含特定数据类型的列。代码和结果如下:

import pandas as pd
import re

data = pd.read_csv('SevAvail2.txt', sep="\t", header=None)
df = pd.DataFrame(data)


header = df.column = df.iloc[0]
header = df.reindex(df.index.drop(0))

# print(header)
df = header
df = df.loc[:, df.isnull().mean() < .95]

#count remaining column length and print list with count
col_length = len(df.columns)
print(col_length)
header_label = []
for i in range(0, col_length):
    header_label.append(i)

#reset headers to (0 : n)
df.columns = header_label

# print(df)
for column in df.columns[0:]:
    print(df[column])

结果列:

1     AB21313BF
2     AB21313GF
3     AB21313SF
4     AB21313CF
5     AB21313KF
Name: 0, dtype: object

1          BABA TECH
2              LALA TECH
3              NDMP
4          IND CORP
5          CAMP 
Name: 1, dtype: object

1       9.2500
2      15.7500
3       7.0000
4      19.7500
5      33.5000
Name: 2, dtype: object

1         -65
2        1.75
3           0
4          -4
5        .75)
Name: 3, dtype: object

1      4,501,561.00 
2      3,145,531.00 
3      1,454,303.00 
4      1,420,949.00 
5      1,095,575.00 
Name: 4, dtype: object

2 个答案:

答案 0 :(得分:0)

您可以使用pandas infer_dtype API来推断列的数据类型。

示例:

import pandas as pd
df = pd.DataFrame({'c1': [1,2], 'c2': [1.0,2.0], 'c3': ["a","b"]})
for c in df.columns:
    print (pd.lib.infer_dtype(df[c]))

输出:

integer floating string

文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.api.types.infer_dtype.html

以字符串形式存储的数字:

当数字包含“,”并存储为字符串(例如:'4,501,561.00')时,一种暴力方式是

import pandas as pd
df = pd.DataFrame({'c1': ['4,501,561.00','501,561.00'], 'c2': [1.0,2.0], 'c3': ["a","b"]})
for c in df.columns:
    if pd.lib.infer_dtype(df[c]) == 'string':
        # Or is it a number stored as string 
        try:
            df[c].str.replace(',','').astype(float)
            print ("floating")
        except:
            print ("string")
    else:
        print (pd.lib.infer_dtype(df[c]))

答案 1 :(得分:0)

如果它应该是一个数字,并且python被识别为对象,则表示该字段中包含非数字字符。您可以手动查看源文件,也可以强制您认为应该为数字的列。另外,您可以通过在read语句中分配数据类型来强制导入数据类型

pr.read_csv('filename', sep='/t', dtype= {'Field1':int, 'Field2':str... }

以此类推...

相关问题