我正在尝试从我的气象站记录条目。我可以在 Pandas 数据框中收集我需要的数据,但我必须将其转换为特定的列表格式,才能使用 Gspread 将多个输入附加到 Google 表格中。
dataframe.to.list() 函数给了我这种格式:
Name=Xiaoling
等
但是 Gspread 的附加函数需要这种格式:
class Solution {
private static class Person {
private String Name = "Unknown";
private Map<String, String> Traits = new TreeMap<>();
}
// constructs person from given input
private static Person ConvertRawInputToPerson(String rawInput) {
Person person = new Person();
String[] traits = rawInput.split(" ");
for (String trait : traits) {
String[] keyValuePair = trait.split("=");
String key = keyValuePair[0];
String value = keyValuePair[1];
if (key.equals("Name")) {
person.Name = value;
} else {
person.Traits.put(key, value);
}
}
return person;
}
private static boolean StringEquals(String left, String right) {
return left.equals(right);
}
private static boolean HasTrait(Map<String, String> traits, Map.Entry<String, String> soughtTrait) {
for (Map.Entry<String, String> trait : traits.entrySet()) {
if (StringEquals(trait.getKey(), soughtTrait.getKey())
&& StringEquals(trait.getValue(), soughtTrait.getValue())) {
return true;
}
}
return false;
}
static class Registry {
List<Person> mPersons = new ArrayList<>();
void add(Person person) {
mPersons.add(person);
}
Person findMatchingPersonByRawTraits(String rawTraits) {
Person soughtPerson = new Person();
String[] traits = rawTraits.split(" ");
for (String trait : traits) {
String[] keyValuePair = trait.split("=");
String key = keyValuePair[0];
String value = keyValuePair[0];
soughtPerson.Traits.put(key, value);
}
for (Person person : mPersons) {
for (Map.Entry<String, String> soughtTrait : soughtPerson.Traits.entrySet()) {
if (HasTrait(person.Traits, soughtTrait)) {
return person;
}
}
}
return new Person();
}
}
// Entry point
private static String GetNameFromRegistryByTraits(String[] rawRegistry, String soughtTraits) {
Registry registry = new Registry();
for (String rawInput : rawRegistry) {
registry.add(ConvertRawInputToPerson(rawInput));
}
Person person = registry.findMatchingPersonByRawTraits(soughtTraits);
return person.Name;
}
而且我相信我一次只能输入一个列表项。
我喜欢获得有关如何将列表格式转换为指定的 Gspread 格式以及如何以自动方式附加所有数据点的帮助。我的知识有限,认为这需要 For 循环。
答案 0 :(得分:0)
import gspread
import gspread_dataframe as gd
gc = gspread.service_account(filename=r'json file path from google api')
sheet = gc.open("File name").worksheet('Sheet1')
Google_frame = gd.get_as_dataframe(sheet)
# find the index of the first null row
idx = existing[existing['some column name'].isna()].head(1).index[0]
# the length of the frame you want to append to the Google sheet plus the index
# df is the frame that you want to append to the google sheet
idx_to = len(df) + idx
# use iloc to append the two frames together
Google_frame.iloc[idx:idx_to, :] = df.to_numpy()
# update the Google Sheet
gd.set_with_dataframe(sheet, Google_frame)