如何 return 在 pandas 数据框中指定单元格上方和下方的 3 列?
How to return 3 columns above and below the specified cell in a pandas dataframe?
这是我的数据框。如果我搜索爱荷华州,代码应该 return 国家名称(在本例中为美国)及其上方的 3 个州(夏威夷、加利福尼亚、密苏里)-爱荷华州- 以及下方的 3 个州(科罗拉多、阿拉斯加、得克萨斯州)这个案例)。如何做到这一点?
Country
States
India
Banglore
Pune
Delhi
Maharasthra
Hyderabad
Gujarat
USA
Arizona
Hawaii
California
Missouri
Iowa
Colorado
Alaska
Texas
试试这个:
import pandas as pd
# prepare the df
c = ['India', '', '', '', '', '', 'USA', '', '', '', '', '', '', '']
s = ['Banglore', 'Pune', 'Delhi', 'Maharasthra', 'Hyderabad', 'Gujarat',
'Arizona', 'Hawaii', 'California', 'Missouri', 'Iowa', 'Colorado', 'Alaska', 'Texas']
df = pd.DataFrame(c, columns=['Country'])
df['State'] = s
df['Country'][1:6] = 'India'
df['Country'][7:14] = 'USA'
print(df)
def get_states(data, state_name):
# find the country
result = data[data['State'] == state_name]
country_name = list(result['Country'])[0]
# filter df by country
all_states = data[data['Country'] == country_name]
l = list(all_states['State'])
index = l.index(state_name)
start = index - 3
end = index + 4
if index < 3:
start = 0
if index + 4 > len(l):
end = len(l)
final_list = l[start:end]
return final_list
final_l = get_states(df, 'Iowa')
print(final_l)
考虑到您的 df
是:
In [814]: df
Out[814]:
Country State
0 India Banglore
1 India Pune
2 India Delhi
3 India Maharasthra
4 India Hyderabad
5 India Gujarat
6 USA Arizona
7 USA Hawaii
8 USA California
9 USA Missouri
10 USA Iowa
11 USA Colorado
12 USA Alaska
13 USA Texas
使用df.iloc
:
In [815]: ix = df[df.State.eq('Iowa')].index[0]
In [816]: num = 3
In [817]: res = df.iloc[ix - num: ix + num + 1]
In [818]: res
Out[818]:
Country State
7 USA Hawaii
8 USA California
9 USA Missouri
10 USA Iowa
11 USA Colorado
12 USA Alaska
13 USA Texas
这是我的数据框。如果我搜索爱荷华州,代码应该 return 国家名称(在本例中为美国)及其上方的 3 个州(夏威夷、加利福尼亚、密苏里)-爱荷华州- 以及下方的 3 个州(科罗拉多、阿拉斯加、得克萨斯州)这个案例)。如何做到这一点?
Country | States |
---|---|
India | Banglore |
Pune | |
Delhi | |
Maharasthra | |
Hyderabad | |
Gujarat | |
USA | Arizona |
Hawaii | |
California | |
Missouri | |
Iowa | |
Colorado | |
Alaska | |
Texas |
试试这个:
import pandas as pd
# prepare the df
c = ['India', '', '', '', '', '', 'USA', '', '', '', '', '', '', '']
s = ['Banglore', 'Pune', 'Delhi', 'Maharasthra', 'Hyderabad', 'Gujarat',
'Arizona', 'Hawaii', 'California', 'Missouri', 'Iowa', 'Colorado', 'Alaska', 'Texas']
df = pd.DataFrame(c, columns=['Country'])
df['State'] = s
df['Country'][1:6] = 'India'
df['Country'][7:14] = 'USA'
print(df)
def get_states(data, state_name):
# find the country
result = data[data['State'] == state_name]
country_name = list(result['Country'])[0]
# filter df by country
all_states = data[data['Country'] == country_name]
l = list(all_states['State'])
index = l.index(state_name)
start = index - 3
end = index + 4
if index < 3:
start = 0
if index + 4 > len(l):
end = len(l)
final_list = l[start:end]
return final_list
final_l = get_states(df, 'Iowa')
print(final_l)
考虑到您的 df
是:
In [814]: df
Out[814]:
Country State
0 India Banglore
1 India Pune
2 India Delhi
3 India Maharasthra
4 India Hyderabad
5 India Gujarat
6 USA Arizona
7 USA Hawaii
8 USA California
9 USA Missouri
10 USA Iowa
11 USA Colorado
12 USA Alaska
13 USA Texas
使用df.iloc
:
In [815]: ix = df[df.State.eq('Iowa')].index[0]
In [816]: num = 3
In [817]: res = df.iloc[ix - num: ix + num + 1]
In [818]: res
Out[818]:
Country State
7 USA Hawaii
8 USA California
9 USA Missouri
10 USA Iowa
11 USA Colorado
12 USA Alaska
13 USA Texas