Reputation: 1202
For most of you, this question seems easy but for beginners like me, its kind of difficult.
I have a dataframe like this:
Voltage Current Kilo Watt KVA Power
423.83772 79.33 38.915012 58.08210746 asf
422.90244 33.73 23.3185 27.43352941 0.85
422.57336 92.29 50.75332 67.67109333 0.75
429.29352 38.5 24.63797 28.64880233 0.86
423.61256 86.49 45.14188 63.58011268 0.71
423.61256 86.49 dfhfgj 63.58011268 0.71
422.91976 36.89 21.20996 24.66274419 0.86
427.18048 88.19 45.815193 64.52844085 0.71
427.12852 64.25 21.9954 47.81608696 pqr
422.46944 75.6 36.11695 55.56453846 0.65
426.66088 32.65 20.36856 24.24828571 0.84
406.88144 304.31 85.102684 212.75671 0.4
412.25064 142.62 92.578545 107.6494709 0.86
412.0428 75.5 36.51638 52.92228986 0.69
422.20964 79.3 39.08528 58.33623881 xyz
421.20508 93.71 ggg 67.97310132 0.76
425.82952 136.95 106.97365 534.86825 0.2
I want to drop the respective rows which has string values.
I tried:
df[df != 0.].dropna(axis=0)
This drops the rows with zeros and errors.
how can i achieve this in python?
Upvotes: 0
Views: 423
Reputation: 224
first convert rows into columns.
df = pd.DataFrame(dict(A=[5,3,5,6], C=[1,"bar","fooXYZbar", "bat"]))
df._get_numeric_data()
result:
A
0 5
1 3
2 5
3 6
Upvotes: 0
Reputation: 76917
Here's a one-liner.
In [882]: df.apply(pd.to_numeric, errors='coerce').dropna()
Out[882]:
Voltage Current Kilo Watt KVA Power
1 422.90244 33.73 23.318500 27.433529 0.85
2 422.57336 92.29 50.753320 67.671093 0.75
3 429.29352 38.50 24.637970 28.648802 0.86
4 423.61256 86.49 45.141880 63.580113 0.71
6 422.91976 36.89 21.209960 24.662744 0.86
7 427.18048 88.19 45.815193 64.528441 0.71
9 422.46944 75.60 36.116950 55.564538 0.65
10 426.66088 32.65 20.368560 24.248286 0.84
11 406.88144 304.31 85.102684 212.756710 0.40
12 412.25064 142.62 92.578545 107.649471 0.86
13 412.04280 75.50 36.516380 52.922290 0.69
16 425.82952 136.95 106.973650 534.868250 0.20
Upvotes: 1